Repository navigation
fix(pytest): report setup errors in the summary - #4466
Conversation
A run with fixture, teardown or collection errors ended "1 passed, 1 error" (exit 1) but rtk printed "Pytest: 1 passed": the summary parser had no arm for error(s) and the ERRORS section was skipped. Count errors, keep the run off the all-green line, and render the ERRORS blocks and ERROR short-summary lines as [ERROR] entries. Fixes rtk-ai#4426
📊 Automated PR Analysis
SummaryFixes the pytest filter so runs with setup/teardown/collection errors are no longer reported as fully green. Review Checklist
Linked issues: #4426 Analyzed automatically by wshm · This is an automated analysis, not a human review. |
Protocol-zero-0
left a comment
There was a problem hiding this comment.
Reviewed cdad612 against eb225a1 with real pytest 9.0.3, including the CLI exit status. I reproduced the original bug on the base: exit 1, but output Pytest: 1 passed in ... with the fixture error hidden. The PR head reports the error count and traceback correctly.
Actual pytest runs on the head:
| Case | Compact result | Raw / RTK exit |
|---|---|---|
| Setup error + passing test | 1 passed, 0 failed, 1 error | 1 / 1 |
| Teardown error + passing test | 2 passed, 0 failed, 1 error | 1 / 1 |
| Setup + teardown errors + assertion failure | 2 passed, 1 failed, 2 errors | 1 / 1 |
Collection error with --continue-on-collection-errors |
1 passed, 0 failed, 1 error | 1 / 1 |
Setup error with -rE |
Error block and ERROR nodeid both present |
1 / 1 |
| Two errors, no passing tests | Raw fallback retains both errors and traceback | 1 / 1 |
| Passing test only | 1 passed | 0 / 0 |
The 18 pytest-filter unit tests also passed. I did not find a blocking issue in the changed error-handling path. The errors-only fallback behaves as described in the PR, so widening summary detection can remain separate.
Small reproduction for the setup case:
import pytest
@pytest.fixture
def boom():
raise RuntimeError("fixture")
def test_error(boom): pass
def test_ok(): passRun in a directory containing that file as test_case.py:
pytest -q --tb=short -rxX
rtk pytest -q --tb=short -rxXBoth should exit 1; the RTK output should retain 1 error and RuntimeError: fixture.
|
I checked this alongside #4374 because both change The current |
|
Updating my earlier integration-order recommendation with new evidence: #3355 now has b4c642c, addressing the previously outstanding parser review. I independently verified that head plus this PR, including a fixture-only error that prints a captured fake-success summary: the combined output is correctly I recommend #3355 first, followed by rebasing this PR onto it for error counts. The combined tree applies cleanly and passes full local checks; review and precise validation scope: #3355 (review). The no-stats-line |
aeppling
left a comment
There was a problem hiding this comment.
Checked against real pytest 7.4.4, 8.0.2 and 9.1.1: setup, teardown and collection errors are now counted and shown as [ERROR] blocks, and exit codes match raw pytest everywhere. On 1,500 fuzzed runs it lowers runs with a missing error count from 546 to 11.
The only regressions are in -qq, where pytest prints no result line and a test printing 1 passed, 1 error gets picked up as the summary. That's the #2960 family, and it's fixed separately.
Nits, not blocking: the two new comments can go, and with -ra each error is listed twice.
Thanks @Cedric921, and @Protocol-zero-0 for the review. Approving. Best merged after #3355.
Summary
A pytest run with fixture, teardown or collection errors ends with
1 passed, 1 error in 0.02s(exit 1), butrtk pytestprintedPytest: 1 passed in 0.02s, so a failing run looked fully green. Three changes insrc/cmds/python/pytest_cmd.rsfix this:parse_summary_linenow countserror/errors, andbuild_pytest_summaryno longer takes the all-green branch when errors are present. The summary line uses the existing multi-count form plus pytest's singular/plural noun:Pytest: 1 passed, 0 failed, 1 error in 0.02s.=== ERRORS ===section now feeds the same block collection as=== FAILURES ===. Its___ ERROR at setup of test_e ___/___ ERROR collecting test_bad.py ___headers render as[ERROR] at setup of test_e/[ERROR] collecting test_bad.py, followed by the same up-to-3 relevant traceback lines as failures.ERROR <nodeid> - <msg>short-summary lines (printed with-rE/-ra) were already collected but rendered as empty entries. They now render likeFAILEDlines, tagged[ERROR].Output for the repro in the issue:
Not changed: summary-line detection. A run where every test errors (
2 errors in 0.03s, with no passed/failed/skipped) is still not recognised as a summary. It still falls back to the raw output, because the exit code is non-zero and the filter result is "No tests collected", so nothing is hidden. Widening detection would conflict with #4374 and #3355, which both rewrite those lines. (#3355's summary regex already acceptserrors?, so combined with this PR that case would also render compactly.) This PR also does not overlap #4374 (last-summary-wins); both can merge in either order.Fixes #4426
Test plan
Pytest: 1 passed in 0.02s,Pytest: 1 passed, 1 failed in 0.05s, …) and pass with the fix:test_filter_pytest_fixture_error_is_reported: the issue's pytest 9.1.1-q --tb=short -rxXoutputtest_filter_pytest_errors_and_failures_together: setup and teardown errors plus a failure,2 errorspluraltest_filter_pytest_collection_error_is_reported:--continue-on-collection-errorsshapetest_filter_pytest_error_short_summary_line:ERROR nodeid - msgline from-rE/-ratest_parse_summary_line_errorscargo fmt --all --checkcargo clippy --all-targets(zero warnings)cargo test(all pass)-q --tb=shortERRORS layout.