Python "unittest discover" Finds No Tests / Import Errors in CI
unittest’s discovery walks a start directory for files matching a pattern. If the start directory, pattern, or package layout is off, it imports nothing and reports Ran 0 tests - or fails importing a test module.
What this error means
python -m unittest discover prints Ran 0 tests in 0.000s (a silent non-failure that hides untested code), or raises an ImportError while importing a discovered module. The tests exist but discovery does not find or import them.
$ python -m unittest discover
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OKCommon causes
Wrong start directory or filename pattern
Discovery defaults to start dir . and pattern test*.py. Tests named tests_*.py or living under tests/ without pointing discovery there are skipped.
Test package not importable
A test directory without __init__.py (in the default non-top-level mode), or a start dir that is not on sys.path, makes discovery import nothing or fail importing.
How to fix it
Point discovery at the right dir and pattern
python -m unittest discover -s tests -p "test_*.py" -t .Make the test package importable
- Add
__init__.pyto test directories, or run from the project root so imports resolve. - Ensure
-t(top-level dir) is the import root for your package. - Fail CI on
Ran 0 testsso an empty discovery does not look like a pass.
How to prevent it
- Use explicit
-s/-p/-tso discovery is unambiguous. - Keep test filenames matching the discovery pattern.
- Treat
Ran 0 testsas a failure in CI, not a pass.