# pytest Exit Code 5 "No Tests Collected" - Causes & Fix in CI

> pytest exit code 5 means no tests were collected, not that tests passed. Every cause - wrong rootdir, discovery patterns, deselecting markers - with step-by-step CI fixes.

Source: https://latchkey.dev/learn/python/pytest-no-tests-ran-exit-5  
Updated: 2026-06-25

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.

## 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)"
```

> Exit code 5 means zero tests were collected. It is a configuration result, not a passing run, and CI that only checks for a non-zero exit will treat it as success unless you assert on the collected count.

## 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; }
```

## FAQ

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

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
