How to organize test cases with PyUnit

Roy Smith roy at panix.com
Sun Jul 7 14:52:00 EDT 2002


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 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