pytest Exit Codes 0-5 Documentation: What Each Code Means
pytest defines exit codes 0 through 5, each meaning something specific - including code 5 for "no tests collected".
Unlike most tools, pytest assigns a distinct meaning to each of its exit codes, so the number tells you precisely what happened.
What it means
0 = all tests passed; 1 = some tests failed; 2 = interrupted (e.g. Ctrl-C); 3 = an internal pytest error; 4 = a command-line usage error; 5 = no tests were collected.
Common causes
- Exit 5: wrong
testpaths/rootdir or files not namedtest_*.py. - Exit 4: a bad CLI flag or option.
- Exit 2: the run was cancelled or a fixture aborted it.
How to fix it
Map the code to its meaning, then act: for 5, fix discovery (paths, naming, conftest.py); for 4, fix the invocation; for 1, read the failing assertions. These are deterministic test outcomes, not transient.