What decides which tests exist#
pytest collects files that match test_*.py or *_test.py by default, and the python_files setting changes the pattern. pytest conventions Jest's default testMatch takes files inside __tests__ folders and files with a .test or .spec suffix. Jest testMatch A file outside the pattern is not an error to either runner. It is simply not a test.
Fixture: a rename and a constructor#
Python 3.13.18 and pytest 9.0.2 in python:3.13-slim. The suite has two price tests and three refund tests, one of which fails:
$ pytest --collect-only -q -p no:cacheprovider | tail -1
5 tests collected in 0.01s
$ pytest -q -p no:cacheprovider
[…]
FAILED tests/test_refunds.py::test_refund_after_expiry - AssertionError: expi...
1 failed, 4 passed in 0.03s
# commit 2: tests/test_refunds.py renamed to tests/refund_tests.py
$ pytest --collect-only -q -p no:cacheprovider | tail -1
2 tests collected in 0.00s
$ pytest -q ... exit 0
[…]
2 passed in 0.01s
# commit 3: the refund tests come back as a class with an __init__
$ pytest --collect-only -q -p no:cacheprovider | tail -1
2 tests collected in 0.01s
$ pytest -q -p no:cacheprovider
/tmp/tmp.ErHWyIMHfI/tests/test_refunds.py:1: PytestCollectionWarning: cannot collect test class 'TestRefunds' because it has a __init__ constructor (from: tests/test_refunds.py)
2 passed, 1 warning in 0.01s
exit 0
The rename lost three tests, including the failing one, and the exit code went from failure to success. The class with a constructor lost its test with a warning that most CI logs bury. The summary lines 2 passed and 1 failed, 4 passed are both accurate; only the count across runs shows the loss.
pytest does flag the extreme case. A directory with no matching file at all printed no tests ran and exited 5, which pytest documents as "No tests were collected". pytest exit codes A partial loss gets no exit code of its own.
Make the loss fail the job#
Turn the collection warning into an error. In a second fixture run, this exited 2 with 1 error during collection:
pytest -W error::pytest.PytestCollectionWarning
Then compare counts. Save the collected count as a job output or artifact on the base branch and fail when the pull request's count is lower:
count=$(pytest --collect-only -q | tail -1 | grep -oE '^[0-9]+')
test "$count" -ge "$BASE_COUNT" || { echo "collected $count, base had $BASE_COUNT"; exit 1; }
A pull request that deletes tests on purpose fails this check too. That is the point: the author says so in the description and the reviewer accepts the new count.
Read a count change#
| Count change | Common cause | What settles it |
|---|---|---|
| Lower, test files deleted in the diff | Intentional removal | The diff |
| Lower, no deletions | Rename out of the pattern, collection error, changed python_files or testMatch | --collect-only on both commits, diffed by test ID |
| Lower, only in some jobs | A shard or matrix leg that did not run | A shard missing from the merged report |
| Same, but a test you expected is absent | One test added, another lost | The diff of the two test ID lists |
The last row is why a count alone is not enough on a busy branch. Keep the list of test IDs, not only the number, so the comparison can name what disappeared.
When a filter, not discovery, ran fewer tests, read a test command that ran no tests.