[Tutor] testing new style class properties using pyUnit ?
Duncan Gibson
duncan at thermal.esa.int
Thu Jul 1 12:08:31 EDT 2004
In my original question, I wrote:
> I had a class which used get and set methods to access a class variable.
> I set up some unit tests using pyUnit.
>
> I've just upgraded the source to use the new style classes with
> properties instead of explicit get and set methods. However, so far I've
> been unable to convert the unit tests. I note the error message, but I
> don't understand why.
>
> Can anyone explain the magical incantation that I need to test the
> property setter?
Well, many thanks to Danny Yoo for pointing me in the right direction,
explaining the name mangling that gives rise to _className__thingName
See his reply for details.
However, I also included the oh-so-glib statement:
> In fact it's worse than this because my real code uses class variables
> and classmethods, and I want to test the derived classes, but the principle
> is the same.
Boy, oh, boy. The naming principle is certainly the same, but after
spending most of the day failing to convert the pyUnit tests to give
the results I expected when using the mangled names, it finally hit me.
The new Python features involving __slots__ and __var = property(...)
apply to instance attributes. Let me say that again: instance attributes.
On first sight they appeared to work with my class variables, and had
it not been for pyUnit I would have thought they were working perfectly,
and six months down the line there would have been a lot of tearing of
hair and gnashing of teeth.
Cheers
Duncan
More information about the Tutor
mailing list