Skip to content
Latchkey

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

  1. Confirm test files follow the test_*.py naming convention.
  2. Pass the tests path explicitly or set testpaths.
  3. 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_*.py

How to prevent it

  • Follow test_*.py naming so discovery finds your tests.
  • Set testpaths so CI looks in the right place.
  • Fail the build on exit code 5 to catch silent no-collection.

Related guides

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