Currently my code is organized in the following tree structure:
src/
module1.py
module2.py
test_module1.py
test_module2.py
subpackage1/
__init__.py
moduleA.py
moduleB.py
test_moduleA.py
test_moduleB.py
Where the module*.py
files contains the source code and the test_module*.py
contains the TestCase
s for the relevant module.
With the following comands I can run the tests contained in a single file, for example:
$ cd src
$ nosetests test_filesystem.py
..................
----------------------------------------------------------------------
Ran 18 tests in 0.390s
OK
How can I run all tests? I tried with nosetests -m 'test_.*'
but it doesn't work.
$cd src
$ nosetests -m 'test_.*'
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Thanks
Whether you seperate or mix tests and modules is probably a matter of taste, although I would strongly advocate for keeping them apart (setup reasons, code stats etc).
When you're using nosetests, make sure that all directories with tests are real packages:
This way, you can just run
nosetests
in the toplevel directory and all tests will be found. You need to make sure thatsrc/
is on thePYTHONPATH
, however, otherwise all the tests will fail due to missing imports.This is probably a hotly-contested topic, but I would suggest that you separate your tests out from your modules. Set up something like this...
Use
setup.py
to install these into the system path (or you may be able to modify environment variables to avoid the need for an "install" step).Now any python script anywhere can access those modules, instead of depending on finding them in the local directory. Put your tests all off to the side like this:
I'm not sure about your
nosetests
command, but now that your tests are all in the same directory, it becomes much easier to write a wrapper script that simply imports all of the other tests in the same directory. Or if that's not possible, you can at least get away with a simplebash
loop that gets your test files one by one:I don't know about nosetests, but you can achieve that with the standard unittest module. You just need to create a
test_all.py
file under your root directory, then import all your test modules. In your case:each module should provide the following function (example with a module with two unit tests:
Class1
andClass2
):I'll give a Testoob answer.
Running tests in a single file is like Nose:
To run tests in many files you can create suites with the Testoob collectors (in each subpackage)
and
I haven't tried your specific scenario.
If they all begin with
test
then justnosetest
should work. Nose automatically searches for any files beginning with 'test'.