Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`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 nosetests 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:

myproj/
    reporter/
    __init__.py
    report.py
    [other app modules]
        tests/
        __init__.py
        test-report.py
        [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.

like image 818
blokeley Avatar asked Feb 23 '11 09:02

blokeley


People also ask

What is Python Unittest discover?

unittest provides a base class, TestCase , which may be used to create new test cases. test suite. A test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.

Is Pytest better than Unittest?

Which is better – pytest or unittest? Although both the frameworks are great for performing testing in python, pytest is easier to work with. The code in pytest is simple, compact, and efficient. For unittest, we will have to import modules, create a class and define the testing functions within that class.

Can we use Pytest and Unittest together?

pytest supports running Python unittest -based tests out of the box. It's meant for leveraging existing unittest -based test suites to use pytest as a test runner and also allow to incrementally adapt the test suite to take full advantage of pytest's features.

How do I run Unittest tests?

If you're using the PyCharm IDE, you can run unittest or pytest by following these steps: In the Project tool window, select the tests directory. On the context menu, choose the run command for unittest . For example, choose Run 'Unittests in my Tests…'.


3 Answers

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. test-report.py 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.

like image 146
ncoghlan Avatar answered Oct 07 '22 17:10

ncoghlan


I had this problem because some directories in a project were missing __init__.py. I thought I don't need them in Python 3.7.

Just add __init__.py to every directory and python3 -m unittest will find tests automatically.

like image 42
Max Malysh Avatar answered Oct 07 '22 17:10

Max Malysh


As someone relatively new to Python, the naming convention in the docs implied the opposite. Ben's comment was very helpful: the default discovery pattern looks for test-modules prefixed with the string "test"

I thought the introspection would just look for class names and not require a specific file naming convention.

Here is what the docs say: https://docs.python.org/3/library/unittest.html python -m unittest discover -s project_directory -p "_test.py" I couldn't get this to work, but by changing my file names to be "test_.py" - success!

like image 25
Bill K Avatar answered Oct 07 '22 19:10

Bill K