Rollback via Lock File vs. Forward Fixing with pytest --collect-only: Choosing a Recovery Strategy for Test Collection Correctness
0 reputation · 31 May 2023, 22:00 UTC
0 reputation · 31 May 2023, 22:00 UTC
The goal is to recover from a failed pytest upgrade while guaranteeing that the test suite discovers exactly the same tests it did before the upgrade, preserving test collection correctness.
Recovery depends on having a reproducible environment (e.g., a lock file or virtualenv) and on correctly diagnosing whether test failures stem from pytest API changes or from unrelated environmental issues. Rolling back to the previously pinned version restores the exact pre‑upgrade API but may reintroduce version conflicts with other dependencies. Forward fixing uses pytest’s built‑in collection verification (--collect-only) to detect and address collection breakage without downgrading, yet it assumes that all failures are due to API changes and may miss non‑collection problems.
Which approach guarantees that the post‑recovery test collection matches the pre‑upgrade collection without requiring changes to test files?
When relying on pytest --collect-only to spot collection breakage, what additional checks are needed to ensure that non‑collection test failures are not overlooked?
Under what circumstances does rolling back to a pinned version introduce dependency conflicts that could compromise test collection correctness?
The only method that guarantees the post-recovery test collection matches the pre-upgrade collection without editing test files is to roll back to the previously pinned pytest version (using a lock file or a saved virtualenv). This restores the exact API and collection semantics that produced the baseline.
Forward fixing relies on pytest --collect-only to detect collection breakage. If the new pytest version intentionally changed collection behavior (for example, stricter duplicate-basename handling or altered import modes), fixing the symptoms may not restore the original node-ID set, so collection equivalence is not assured.
pytest --collect-only -q > baseline.txt
sort -u baseline.txt > baseline.sortedpytest --collect-only -q > recovered.txt
sort -u recovered.txt > recovered.sorteddiff -u baseline.sorted recovered.sorted--collect-only--collect-only does not exercise fixtures, hooks, or test bodies.conftest.py and test module imports, because --collect-only still imports them and import-time side effects execute.pytest>=8 while you pin pytest<8).After a rollback, run pip check (or your lock-file tool's verify command) to surface any such conflicts before trusting the recovered environment.
Do you have a saved lock file or environment snapshot from before the upgrade? If not, rollback is not possible and forward fixing (with the additional checks above) becomes the only viable path.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.