[TIP] unittest issues and development

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed Feb 10 17:53:51 PST 2010


On 12:24 am, fuzzyman at voidspace.org.uk wrote:
>On 11/02/2010 00:20, Fernando Perez wrote:
>>[snip...]
>>>>as well as top-level names for the various
>>>>assertions?  Nose.tools does that in a very simple but useful manner
>>>>that makes lightweight (non-class-based) tests really nice to write:
>>>>
>>>>def test_foo():
>>>>    nose.tools.assert_equals(bar, baz, 'nice message)
>>>I've never seen the advantage of this over self.assertEquals 
>>>(ignoring the
>>>PEP 8 issue...). :-)
>>Ah, they matter if you aren't writing a class: there is no 'self' in
>>the function above.  One very nice thing about such tests in the
>>contexts I work in (scientists who don't necessarily care or want to
>>know about objects) is that the above is a valid test that states
>>precisely the desired goal with pretty much zero boilerplate.
>
>I still don't see the following two lines as 'boilerplate'... :-)
>
>import unittest
>class SomeTest(unittest.TestCase):
>...
>>If I have to tell scientists that before writing a test they have to
>>subclass some special contraption from unittest, and know about self,
>>etc, I'd have to postpone testing until classes have  been somewhat
>>introduced.   But the above approach lets me put tests in pretty much
>>from day 1, which is good.
>
>That I can understand. I don't think there is really much of a solution 
>(except perhaps the solutions you already have...) as unittest is 
>pretty 'object-oriented' in its approach.

I don't think anything in unittest would prevent you from writing a 
loader that could create cases from free functions like the ones above.

Nor would anything prevent you from writing a module with a lot of free 
assert functions for use in those tests.

I don't think I'd do that, though.  If your "scientist" can't wrap his 
or her head around subclassing TestCase, then there's probably going to 
be a lot of other things wrong with any code they end up writing too, 
and you should probably have a programmer helping them out anyway.

Jean-Paul



More information about the testing-in-python mailing list