Skip to content
Latchkey

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.

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

# or
collected 0 items
$ echo $?
5

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 ]

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.

Related guides

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