How to organize test cases with PyUnit

Peter Hansen peter at engcorp.com
Mon Jul 8 00:02:40 EDT 2002


Roy Smith wrote:
> 
> Peter Hansen <peter at engcorp.com> wrote:
> > Rename the runTest()
> > method to start with the text "test", and then change the __main__
> > content to be simply 'unittest.main()' instead of the dictEntries.run
> > that you have.
> 
> OK, that seemed to work, even though I'm still not sure I understand all
> the subtleties of what's going on, and what role all the various classes
> (TestCase, TestSuite, TestResult, TestLoader, etc) play.

I don't really either, but I've never needed to.  Basically just subclass
TestCase and do the above and you may never need to either. :)

> After writing just a few test cases, I've come to the conclusion that
> the names of the individual test methods are meaningless, except to
> dictate what order the tests run in.  I've taken to just naming them
> test101, test102, etc (leaving plenty of room in the numbering system to
> insert additional tests).  The doc strings seem to be the place to get
> creative, since those are what's printed.

Is it really important the order the tests are run in?  Sometimes
it is for me, though rarely, so in those cases I'll prefix the
name with test01 or whatever, but I still put a useful name in it
like test01_CheckLength()...  I never like to feel like I'm doing
extra/wasteful work to maintain things, like "leaving plenty of room"
to insert additional tests.

> I don't see it mentioned in the documentation, but it seems that if you
> make multiple subclasses of unittest.TestCase, the names of those
> classes get sorted and run in order as well.  So, I'm inclined to adopt
> the same scheme of just naming the classes class101, etc.

I thought they were run in order of definition in the file, though I
could be wrong.  Since I *never* care about the order of the TestCases,
though, I just keep useful names for them too, instead of numbers.

-Peter



More information about the Python-list mailing list