Yes—a pytest run can fail even when other collected tests pass. Exit code 1 means tests were collected and run, but some failed; it does not mean every test failed. The session result and each test’s result are different things.
Why pytest can fail when some tests pass
Pytest reports a session-level exit code as well as individual test outcomes. A session can include both passing and failing tests, and still exit with code 1. The official exit-code reference distinguishes that result from a fully passing run and from a run with no collected tests.
As an Amazon Associate I earn from qualifying purchases.
| Exit code | Meaning |
|---|---|
0 |
All tests were collected and passed. |
1 |
Tests were collected and run, but some failed. |
5 |
No tests were collected. |
So “pytest failed” describes the invocation, not necessarily every test item in it. In a CI job, inspect the test results or failure report to see which items passed and which failed; the exit code alone does not provide that breakdown.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteChoose a rerun option based on what you need to run
Pytest’s rerun options differ in whether they run only remembered failures, the entire suite, or stop at the next failure. These options rely on pytest’s cache, so check the installed version and project configuration before assuming identical behavior across repositories.
#1 Best Overall
| Command | What it runs | Important distinction |
|---|---|---|
pytest --lf or pytest --last-failed |
Tests pytest remembers as failed in the previous run. | With no remembered failures, the default is to run all tests. --last-failed-no-failures can change the no-known-failures behavior to all or none. |
pytest --ff or pytest --failed-first |
The full suite, with remembered failures scheduled first. | This is not a failures-only retry. The changed order can affect fixture setup and teardown. |
pytest --sw or pytest --stepwise |
Tests until a failure occurs; a later invocation continues from the last failing test. | Useful for working through failures incrementally, but not a normal full-suite run. |
Pytest documents cache inspection with pytest --cache-show and cache removal with pytest --cache-clear. Clearing the cache removes the remembered state that influences options such as --lf and stepwise execution. The pytest cache guide explains the cache behavior.
What it means when a failed test passes on retry
A test that fails intermittently or sporadically is flaky, in pytest’s terminology. A passing retry may be evidence of intermittent behavior, but it does not reveal the cause or prove the original failure was harmless. Pytest’s flaky-test guidance identifies uncontrolled system state as a broad source of inconsistency.
Investigate what differed between the failing and passing attempts rather than treating the retry as a fix. In particular, check:
- Cleanup and test data left behind by earlier tests.
- Whether the result changes with test order or when tests run in parallel.
- Shared state or thread-safety issues.
- Assertions that may be stricter than the behavior being tested warrants.
- Environmental conditions that vary between attempts.
Reruns can be useful in CI: pytest says that “Rerunning any failed tests can mitigate the negative effects of flaky tests by giving them additional chances to pass, so that the overall build does not fail.” That is a mitigation, not a diagnosis. A retry that passes does not establish that the underlying inconsistency has been resolved.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




