Code Management

misc at hl.id.au misc at hl.id.au
Wed Nov 28 19:25:50 CET 2007


On Nov 20, 4:09 pm, Jens <j3n... at gmail.com> wrote:
>
> 1) Should I put my unittests in a subdirectory? Does the subdirectory
> have to be a package?

As others have suggested, this is a good way to organise your tests.
To avoid problems with the import path, look at nosetests [1]. This
allows you to run::

  nosetests
main_package_name.subpackage1.test.test1:TestSomeClass.test_func

to run the test_func function in the TestSomeClass test case.

Or alternatively::

  nosetests main_package_name.subpackage1

to run all tests contained in the main_package_name.subpackage1
module.

This allows you to remove all of your 'if __name__ ==
"__main__":unittest.main()' code, as well as gets rid of the problem
with relative imports (which others solved using .pth files).

The only downside for me is that running nosetests takes at least half
a second (eg: import nose), compared to making the tests runnable
which can be _very_ quick. For test driven development it's nice to
have tests really really quick.

One gotcha; if you're converting from a 'if __name__ ==
"__main__":...' system; nosetests by default ignores files that are
executable (so you'll need to chmod -x main_package_name/subpackage1/
test/test1.py).


[1] nose: a discovery-based unittest extension - (http://
somethingaboutorange.com/mrl/projects/nose/).




More information about the Python-list mailing list