Skip to content
Latchkey

pytest Exit Code 5 "No Tests Collected" - Causes & Fix 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.

pytest output
============================ no tests ran in 0.02s =============================
ERROR: file or directory not found: tests/

# or
collected 0 items
$ echo $?
5

Diagnose it: what did pytest actually collect?

Most pytest failures that only happen in CI are collection or import-path problems rather than test failures. The runner has a different working directory, a different sys.path, and usually no editable install, so a test module that imports your package locally may not resolve at all.

Terminal
# what would run, without running it
pytest --collect-only -q | tail -20

# where pytest thinks the root is (drives conftest and import mode)
pytest --collect-only 2>&1 | grep -i rootdir

# is the package importable at all from the runner cwd?
python -c "import yourpackage, sys; print(yourpackage.__file__)"
python -c "import sys; print(sys.path)"

Common 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.

Terminal
pytest --collect-only
pytest --collect-only tests/   # point at the right dir

Fix discovery naming and config

  1. Name files test_*.py and functions test_* (classes Test* with no __init__).
  2. Set testpaths in pyproject.toml/pytest.ini so CI and local agree.
  3. Check addopts, -m, and -k aren’t deselecting everything.

Allow no-tests if intentional

If a subset legitimately has no tests, suppress the exit-5 failure for that step.

Terminal
pytest tests/optional || [ $? -eq 5 ]

Make a zero-test run fail the build

Terminal
# fail explicitly when nothing is collected
pytest --strict-markers -q
test "${PIPESTATUS[0]}" -ne 5 || { echo "pytest collected no tests"; exit 1; }

How to prevent it

  • Set testpaths so 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.

Frequently asked questions

What causes pytest exit code 5 "No tests Collected"?
There are 3 common causes: running from the wrong directory or path, tests don’t match discovery rules, and a marker filter or -k deselected everything. CI runs pytest from a directory where the test path doesn’t exist, or passes a path that doesn’t match where tests live.
How do I fix pytest exit code 5 "No tests Collected"?
There are 3 fixes depending on which cause you have: confirm what pytest sees, fix discovery naming and config, and allow no-tests if intentional. Work through them in order, since the first is the most common.
What does pytest exit code 5 "No tests Collected" actually mean?
pytest prints "no tests ran" and "collected 0 items", and the job fails with exit code 5.
How do I stop pytest exit code 5 "No tests Collected" happening again?
Set testpaths so discovery is explicit and portable. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card