Skip to content

Commit 40451bc

Browse files
authored
chore(ci-insights): Log reruns in pytest report (#282)
The goal of this change is to make reruns visible in the final pytest report. It'll help us debug issues with the flaky detection mechanism. References: MRGFY-6172
1 parent 799a304 commit 40451bc

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

pytest_mergify/__init__.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,34 @@ def _reruntestprotocol(
272272
item=item, nextitem=nextitem, log=False
273273
)
274274
for report in reports:
275-
self.mergify_ci.flaky_detector.try_fill_metrics_from_report(report)
275+
if report.when != "call":
276+
item.ihook.pytest_runtest_logreport(report=report) # Log as usual.
277+
else:
278+
# Make rerun visible in the logs by temporarily changing
279+
# outcome. The goal is to count a potential failure as a rerun
280+
# instead of a regular failure.
281+
original_outcome = report.outcome
282+
report.outcome = "rerun" # type: ignore[assignment]
283+
item.ihook.pytest_runtest_logreport(report=report)
284+
report.outcome = original_outcome
276285

277286
return reports
278287

288+
@pytest.hookimpl
289+
def pytest_report_teststatus(
290+
self,
291+
report: _pytest.reports.TestReport,
292+
) -> typing.Optional[
293+
typing.Tuple[
294+
str, str, typing.Union[str, typing.Tuple[str, typing.Mapping[str, bool]]]
295+
]
296+
]:
297+
# https://github.com/pytest-dev/pytest-rerunfailures/blob/master/src/pytest_rerunfailures.py#L622-L625
298+
if report.outcome == "rerun": # type: ignore[comparison-overlap]
299+
return "rerun", "R", ("RERUN", {"yellow": True}) # type: ignore[unreachable]
300+
301+
return None
302+
279303
@pytest.hookimpl(tryfirst=True)
280304
def pytest_runtest_teardown(
281305
self,

pytest_mergify/flaky_detection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def _fetch_context(self) -> _FlakyDetectionContext:
166166
return result
167167

168168
def try_fill_metrics_from_report(self, report: _pytest.reports.TestReport) -> bool:
169-
if report.outcome not in ["failed", "passed"]:
169+
if report.outcome not in ["failed", "passed", "rerun"]:
170170
return False
171171

172172
test = report.nodeid

tests/test_ci_insights.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,12 @@ def test_quux():
269269
# The goal is to make sure the failed rerun of the flaky test does not
270270
# impact the results to avoid failing the CI of our users.
271271
assert result.ret == 0
272-
result.assert_outcomes(
273-
passed=4, # Initial run of each test.
274-
skipped=1,
275-
)
272+
273+
outcomes = result.parseoutcomes()
274+
assert len(outcomes) == 3
275+
assert outcomes["passed"] == 4 # Initial run of each test.
276+
assert outcomes["skipped"] == 1
277+
assert outcomes["rerun"] == 3000 # 1000 reruns for each unhealthy test.
276278

277279
assert re.search(
278280
r"""🐛 Flaky detection

0 commit comments

Comments
 (0)