Skip to content
Latchkey

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.

Terminal
$ python -m unittest discover
----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

Common 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

Terminal
python -m unittest discover -s tests -p "test_*.py" -t .

Make the test package importable

  1. Add __init__.py to test directories, or run from the project root so imports resolve.
  2. Ensure -t (top-level dir) is the import root for your package.
  3. Fail CI on Ran 0 tests so an empty discovery does not look like a pass.

How to prevent it

  • Use explicit -s/-p/-t so discovery is unambiguous.
  • Keep test filenames matching the discovery pattern.
  • Treat Ran 0 tests as a failure in CI, not a pass.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →