Skip to content

Commit 4620947

Browse files
remyduthuclaude
andauthored
fix(ci-insights): Preserve initial test status during flaky detection reruns (#358)
In unhealthy mode, the span's `test.case.result.status` was being overwritten with "rerun" during each rerun iteration. Since "rerun" is not a valid status on the server side, these spans were being dropped, causing `rerun_count` to never be recorded for unhealthy tests. This fix ensures span attributes are only set during the initial test run by checking `is_test_rerun()` and returning early for subsequent reruns. The `test.case.result.status` now correctly reflects the initial run's outcome. Fixes: MRGFY-6373 Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 856687b commit 4620947

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

pytest_mergify/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,17 @@ def pytest_runtest_logreport(self, report: _pytest.reports.TestReport) -> None:
364364
if report.outcome is None:
365365
return # type: ignore[unreachable]
366366

367+
if (
368+
self.mergify_ci.flaky_detector
369+
and self.mergify_ci.flaky_detector.is_test_rerun(report.nodeid)
370+
):
371+
return
372+
373+
self._update_current_span_from_report(report)
374+
375+
def _update_current_span_from_report(
376+
self, report: _pytest.reports.TestReport
377+
) -> None:
367378
has_error = report.outcome == "failed"
368379
status_code = (
369380
opentelemetry.trace.StatusCode.ERROR

pytest_mergify/flaky_detection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ def is_test_too_slow(self, test: str) -> bool:
253253
> metrics.remaining_time()
254254
)
255255

256+
def is_test_rerun(self, test: str) -> bool:
257+
"""Returns `True` if the test has already completed its initial run and is
258+
now in a rerun, `False` otherwise."""
259+
return (
260+
metrics := self._test_metrics.get(test)
261+
) is not None and metrics.rerun_count > 1
262+
256263
def is_rerunning_test(self, test: str) -> bool:
257264
return (
258265
metrics := self._test_metrics.get(test)

tests/test_ci_insights.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ def test_quux():
308308
assert not span.attributes.get("cicd.test.new")
309309
assert span.attributes.get("cicd.test.flaky_detection", False) is True
310310
assert span.attributes.get("cicd.test.rerun_count", 0) == 1000
311+
# The status should reflect the initial run outcome, not "rerun"
312+
assert span.attributes.get("test.case.result.status") == "passed"
311313

312314

313315
@responses.activate

0 commit comments

Comments
 (0)