Search code examples
pythonunit-testing

Unittest discovery not working as expected


I have the following project (available on GitHub):

enter image description here

Here's test_some_unit.py:

from ..util.foo import run_foo


def test_some_unit():
    run_foo()
    print("Unit test")

Running discovery for test.unit produces 0 tests:

python -m unittest discover test.unit

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

Why?


Solution

  • Your test cases are module-level functions. pytest supports test cases written in that way, but unittest doesn't. In unittest, test cases are methods of test classes (which inherit from unittest.TestCase). (Reference: "Organizing test code" in module documentation).

    So you would need to reorganize your tests into this style for them to be discovered.