blokeley blokeley -4 years ago 354
Python Question

`python -m unittest discover` does not discover tests

Python's unittest discover does not find my tests!

I have been using nose to discover my unit tests and it is working fine. From the top level of my project, if I run

I get:

Ran 31 tests in 0.390s

Now that python 2.7 unittest has discovery, I have tried using

python -m unittest discover

but I get

Ran 0 tests in 0.000s

My directory structure is:

[other app modules]
[other test modules]

Do you have any ideas why unittest's discovery algorithm can't find the tests?

I'm using Python 2.7.1 and nose 1.0.0 on Windows 7.

Answer Source

The behaviour is intentional, but the documentation could make this clearer. If you look at the first paragraph in the test discovery section, it says For a project’s tests to be compatible with test discovery they must all be importable from the top level directory of the project (in other words, they must all be in Python packages).

A corollary to that is that the file names must also be valid Python module names. fails that test, since test-report is not a legal Python identifier.

A docs bug suggesting that this be mentioned explicitly in the documentation for the -p pattern option would probably be a good way forward.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download