diff --git a/changelog/15277.bugfix.rst b/changelog/15277.bugfix.rst new file mode 100644 index 00000000000..ff38059b7a6 --- /dev/null +++ b/changelog/15277.bugfix.rst @@ -0,0 +1 @@ +Fixed negative durations being reported for subtests when the clock is mocked or frozen. diff --git a/testing/test_subtests.py b/testing/test_subtests.py index f3eab0099c3..3110b22c5e4 100644 --- a/testing/test_subtests.py +++ b/testing/test_subtests.py @@ -1147,3 +1147,24 @@ def test(self): pytester.syspathinsert() result = pytester.runpytest("-n1", "-pxdist.plugin") result.assert_outcomes(passed=2) + + +def test_subtest_with_mocked_clock(pytester: pytest.Pytester) -> None: + pytester.makepyfile( + """ + import time + + def test_with_subtest(subtests, monkeypatch): + real_sleep = time.sleep + monkeypatch.setattr(time, "perf_counter", lambda: 0.0) + monkeypatch.setattr(time, "time", lambda: 0.0) + + with subtests.test("frozen clock"): + real_sleep(0.01) + """ + ) + result = pytester.runpytest("--junitxml=report.xml") + result.assert_outcomes(passed=1) + + xml = pytester.path.joinpath("report.xml").read_text(encoding="utf-8") + assert 'time="-' not in xml