Pytest: no tests ran, exit code 5 (tested)
Pytest exits with code 5 and no tests ran whenever it finds zero tests to run — which has at least three unrelated causes that all print the same bottom line. We reproduced all three on pytest 9.1.1 and measured what distinguishes them.
1. The most common real cause: a naming mismatch
We wrote a file with a real, working function — just not named with pytest's expected prefix:
def check_something():
assert True
$ pytest test_b.py
collected 0 items
no tests ran in 0.01s
Exit code: 5. Pytest only collects functions starting with test (and methods on classes starting with Test) by default — check_something is invisible to it even though the file and function both run fine as plain Python. Renaming to test_something fixed it with no other change: collected 1 item, 1 passed.
2. A -k filter that matches nothing gives the same exit code, different wording
$ pytest -k "nomatch" test_a.py
collected 1 item / 1 deselected / 0 selected
1 deselected in 0.01s
Still exit code 5, but the message is "deselected," not "no tests ran" — the test was found and then filtered out, a different situation from never being collected at all. The fix here is checking the -k expression against the actual test names, not the file.
3. An empty directory or file gives the plain "no tests ran"
$ pytest emptydir/
collected 0 items
no tests ran in 0.01s
Also exit code 5 — the path was valid but genuinely had nothing matching pytest's discovery rules inside it (no files named test_*.py/*_test.py, or none with test-shaped names inside them).
4. The fastest way to tell these apart: --collect-only
We confirmed --collect-only shows exactly what pytest sees without running anything, which immediately tells you whether a test exists and was filtered, or never existed to pytest at all:
$ pytest --collect-only test_c.py
collected 0 items
no tests collected in 0.01s
If a file you know has test code shows 0 collected here, check the naming convention first (function/class/method name prefixes) before suspecting a filter or path problem.
Frequently asked questions
Why does pytest say 'no tests ran' when my test function clearly exists?
Almost always a naming mismatch — we confirmed a function named check_something (instead of test_something) is completely invisible to pytest's default collection, even though it runs fine as plain Python. Pytest only collects functions/methods starting with test by default.
Does a bad -k filter give the exact same message?
Not quite — we measured it says '1 deselected', not 'no tests ran', because the test was found and then filtered out rather than never discovered. Both give exit code 5, but the wording tells you which situation you're in.
What exit code does 'no tests ran' produce?
5, in every cause we measured — the naming-mismatch case, the -k-filtered case, and the empty-directory case all exited 5, even though two of the three print different message text.
How do I quickly check what pytest would collect without running anything?
pytest --collect-only — we confirmed it lists what pytest actually sees as tests with zero execution, which is the fastest way to tell a naming problem (0 collected) from a filtering problem (items found but deselected).
Written with AI assistance. Measured on 2026-10-03 with pytest 9.1.1 on Python 3.13.5, reproducing three distinct causes: a -k filter matching nothing, an empty directory, and test functions/classes not following pytest's naming convention. Every message and exit code below is copy-pasted output, not paraphrased. PlainHeaders is our extension; pytest is free.