[TIP] Functions for tests instead of classes

Mark Sienkiewicz sienkiew at stsci.edu
Thu Mar 4 11:31:19 PST 2010


Michael Foord wrote:
> Hello all,
>
> Some of you are *really* keen to use functions instead of classes for 
> testing.
>
> Attached is an example of one way of doing that using unittest2 and 
> the load_tests protocol.

Perhaps you could make this an intrinsic capability of unittest2, much 
like nose and py.test recognize test functions natively.  If I 
understand it correctly, the whole point of unittest2 is to implement 
the operation "run all the tests in this file", so natively recognizing 
test functions would be a nice idea.

( b.t.w.  I like that nose and py.test recognize test functions, but I 
don't want their integrated test discovery.  I think it is an 
_excellent_ design choice to separate discover (which finds files that 
contain tests) from unittest2 (which runs the tests in a file). )

> At the moment there are no assert functions (I may expand simpletest 
> to include these at some point) so you can use assert statements, with 
> the cost of the useful failure messages unless you construct them 
> yourself.

It never made sense to me that the assert functions in unittest are 
attached to the TestCase object.  You could have a more generally usable 
interface to the module if they were all free standing functions.

Then, to retain compatibility with older versions of unittest, the 
TestCase object can contain a bunch of trivial methods like:

    def assertEqual(self, a, b) :
       return assertEqual(a,b)

This keeps all the capabilities that are in the current implementation, 
but your[1] test functions get to the friendly assertions for free.

Mark S.

[1]  Ok, not _your_ test functions -- _my_ test functions. :)




More information about the testing-in-python mailing list