How to organize test cases with PyUnit

John Roth johnroth at ameritech.net
Sun Jul 7 19:05:55 EDT 2002


"Roy Smith" <roy at panix.com> wrote in message
news:roy-F91625.14520007072002 at reader2.panix.com...
> I'm looking at using unittest for a new project.  This would be my
first
> use of unittest, although I've rolled my own test suites a few times.
> How do people usually organize the test cases for a module?  Offhand,
I
> can think of a few ways to do it.
>
> I could put all the test-case code in the same .py file as the module.
> This seems the least complicated, but perhaps also the least efficient
> since it's a lot more code to parse when the module is imported.
> Possibly isolate the test cases with a __name__ == "__main__"
> conditional?
>
> I could put the test cases for a module in another file in the same
> directory, or possibly put all the test cases for all my modules in a
> "test" subdirectory.  This answers the efficiency question above, but
> seems like it's begging to have the two files get out of sync.  Also,
> does the test case file import the module being tested?  Is there a
> common higher-level file that imports both?

I usually have a test module for each production module. It has
the same name as the production module, suffixed with _test.
There's also a high level test module that imports all of the test
modules and runs them; I call it "System_test".
The skeleton for the test module is pretty standard: it imports
unittest, the module under test, a package of testing subroutines
I've written, and then anything else I need. It ends with the standard
idiom to execute itself when invoked. The rest of the module is the
invocation procedure, and then a class to test each class in the
production module. You will probably want to do this differently;
I don't make very much use of the set_up and tear_down methods.
>
> I realize none of these are absolutely critical issues, but I'd be
> interested to hear how othe people have done it.
>
> BTW, I started out with the test cases in a "test" subdirectory, and
ran
> into a slight uglyness.  My initial idea was to just have
module-test.py
> do an "import ../module", until I discovered that gives you a syntax
> error.  I can get around it with:
>
> import sys
> sys.path.insert (1, '..')
> import module
>
> but that seems clumsy.  Is there a cleaner way to do that?





More information about the Python-list mailing list