Organizing unit test?

Roy Smith roy at
Sun Aug 22 18:14:03 CEST 2010

Over the years, I've tried different styles of organizing unit tests.

I used to create a test directory and put all my tests there.  I would 
maintain a one-to-one correspondence between production source and test 
source, i.e. the test code for would be in test/

More recently, I've taken to having and in the same 

My latest experiment is to just put both the production code and the 
test code in the same file, ending with

if __name__ == '__main__':

So, if I import the file, I get the production code as a module, and if 
I run it from the command line, it runs the tests.  This makes the file 
messier, but it makes the directory structure cleaner and (I think) 
makes the whole thing easier to edit.

Any of these work.  I'm just curious what organizations other people 
have used and what the plusses and minuses ended up being.

More information about the Python-list mailing list