[Tutor] Newbie & Unittest ...
Steven D'Aprano
steve at pearwood.info
Fri May 7 04:20:46 CEST 2010
On Fri, 7 May 2010 03:53:08 am Damon Timm wrote:
> Hi Lie -
>
> Thanks for that idea -- I tried it but am getting an error. I read a
> little about the __dict__ feature but couldn't figure it. I am going
> to keep searching around for how to dynamically add methods to a
> class ... here is the error and then the code.
With respect to Lie, dynamically adding methods is an advanced technique
that is overkill for what you seem to be doing, and the code he gave
you can't work without major modification.
Tests are code too, and the more complicated you make your tests, the
less confidence you should have in them. The more tricks you use
(dynamic methods, metaclasses, complicated frameworks, etc.) the higher
the chances that your test code itself will be buggy, and therefore
your pass/fail results are meaningless. For example, some time ago I
was testing some code I had written, and was very happy that all my
tests were passing. Then I discovered that *dozens* of tests weren't
even being called due to a bug in the framework. When I worked around
that problem, I discovered that now my tests were failing. Because my
framework was complicated, it had a bug in it, which meant my tests
were buggy, which meant my code was buggy and I didn't know.
The lesson is, keep your test code simple. Don't play tricks or be too
clever. Don't trust your test framework, not even well-known ones like
Python's own doctest: you should double check your results, e.g.
sometimes I will add a test I know will fail, just to make sure that
the framework will see it.
--
Steven D'Aprano
More information about the Tutor
mailing list