pytest "collected 0 items" in CI
pytest started cleanly but its discovery rules matched no test files or functions. Either the files are not named for discovery, or pytest looked in the wrong directory.
What this error means
pytest prints "collected 0 items" and exits - sometimes with exit code 5 (no tests) - even though tests exist in the repo.
pytest
============================= test session starts ==============================
rootdir: /home/runner/work/app/app
collected 0 items
============================ no tests ran in 0.02s =============================Common causes
Test files or functions not named for discovery
pytest only collects files matching test_*.py/*_test.py and functions named test_* unless configured otherwise.
pytest invoked from the wrong directory
The command runs where there are no tests, or testpaths points elsewhere, so nothing is found.
How to fix it
Point pytest at the test directory
- Confirm test files follow the
test_*.pynaming convention. - Pass the tests path explicitly or set
testpaths. - Re-run and confirm a non-zero collected count.
Terminal
pytest tests/Declare discovery config
Set testpaths and patterns so discovery is unambiguous in CI.
pytest.ini
[pytest]
testpaths = tests
python_files = test_*.pyHow to prevent it
- Follow
test_*.pynaming so discovery finds your tests. - Set
testpathsso CI looks in the right place. - Fail the build on exit code 5 to catch silent no-collection.
Related guides
pytest exit code 5 (no tests ran) in CIFix pytest exit code 5 in CI - pytest collected no tests and returns 5, which fails the job even though no te…
pytest ModuleNotFoundError during collection in CIFix a pytest ModuleNotFoundError during collection in CI - pytest cannot import your package while loading te…
pytest "ImportError while importing test module" in CIFix pytest "ImportError while importing test module" in CI - a test file failed to import, often from a dupli…