pytest "no tests ran" (Exit Code 5) - Fix Empty Collection in CI
pytest exits with code 5 when it collected zero tests. CI treats that as a failure on purpose - a run that tests nothing should not look green. Usually discovery is pointed at the wrong place or your names don’t match the conventions.
What this error means
pytest prints "no tests ran" and "collected 0 items", and the job fails with exit code 5. Your tests exist, but pytest did not discover any from where it ran.
============================ no tests ran in 0.02s =============================
ERROR: file or directory not found: tests/
# or
collected 0 items
$ echo $?
5Common causes
Running from the wrong directory or path
CI runs pytest from a directory where the test path doesn’t exist, or passes a path that doesn’t match where tests live.
Tests don’t match discovery rules
pytest only collects files like test_*.py/*_test.py and functions named test_*. Misnamed files or classes without the Test prefix are invisible.
A marker filter or -k deselected everything
A -m/-k expression, or addopts in config, can filter out every test, leaving zero collected.
How to fix it
Confirm what pytest sees
List collection without running to see exactly which tests are discovered.
pytest --collect-only
pytest --collect-only tests/ # point at the right dirFix discovery naming and config
- Name files
test_*.pyand functionstest_*(classesTest*with no__init__). - Set
testpathsinpyproject.toml/pytest.iniso CI and local agree. - Check
addopts,-m, and-karen’t deselecting everything.
Allow no-tests if intentional
If a subset legitimately has no tests, suppress the exit-5 failure for that step.
pytest tests/optional || [ $? -eq 5 ]How to prevent it
- Set
testpathsso discovery is explicit and portable. - Follow pytest naming conventions for files, classes, and functions.
- Treat exit 5 as a signal that collection broke, not noise.