Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(devex): stop an empty JUnit freezing every product's test timings (#91602)

Thomas Obermüller38782957b910
diff --git a/.github/scripts/optimize_test_durations.py b/.github/scripts/optimize_test_durations.py
index c8adf2eac3ea..89dd15ed7fe1 100644
--- a/.github/scripts/optimize_test_durations.py
+++ b/.github/scripts/optimize_test_durations.py
@@ -129,6 +129,16 @@ class JUnitShard:
# An XML of this shard (any attempt) did not parse, so call_times is incomplete.
unreadable: bool = False
+ @property
+ def recorded_nothing(self) -> bool:
+ """Every XML parsed and none held a testcase: this shard ran no tests.
+
+ A product with no tests still uploads a JUnit declaring `tests="0"`, so a shard
+ holding only such products reads zero. That is a clock reading of zero rather
+ than a missing clock, which is why it is not `unreadable`.
+ """
+ return not self.unreadable and not self.call_times
+
@classmethod
def load_all(cls, junit_dir: Path, segment: str | None = None) -> list["JUnitShard"]:
"""Load JUnit XMLs and extract per-test call times from each shard.
@@ -948,7 +958,10 @@ def main():
# The check is only as good as its clock. A missing, partial, or
# truncated JUnit set would pass vacuously and let an unchecked slice
# through, so a strict run needs one readable JUnit per timing shard.
- unreadable = [shard.name for shard in junit_shards or [] if shard.unreadable or not shard.call_times]
+ # A shard that parsed and ran no tests counts as read: it reports zero
+ # rather than hiding a number, and refusing it discarded the slice for
+ # every product because one product had no tests yet.
+ unreadable = [shard.name for shard in junit_shards or [] if shard.unreadable]
if not junit_shards:
logger.error("--fail-on-drift needs JUnit artifacts and none loaded")
sys.exit(1)
@@ -971,8 +984,10 @@ def main():
logger.info(" map/clock %s: %.2f", name, ratio)
# A shard with no ratio shares no keys with the map, or only zero times.
# Neither is a pass: it means the clock and the map do not describe the
- # same tests, which is exactly what strict mode is there to catch.
- unrated = [shard.name for shard in junit_shards if shard.name not in ratios]
+ # same tests, which is exactly what strict mode is there to catch. A shard
+ # that ran no tests is the exception: there is nothing to compare, so its
+ # absence from the ratios says nothing about the map.
+ unrated = [shard.name for shard in junit_shards if shard.name not in ratios and not shard.recorded_nothing]
if unrated and args.fail_on_drift:
logger.error("Map/clock ratio missing for %d shards (no overlapping tests): %s", len(unrated), unrated)
sys.exit(1)
diff --git a/.github/scripts/test_optimize_test_durations.py b/.github/scripts/test_optimize_test_durations.py
index 51427fbc6c61..7a94c2143fa4 100644
--- a/.github/scripts/test_optimize_test_durations.py
+++ b/.github/scripts/test_optimize_test_durations.py
@@ -33,6 +33,11 @@ _MIN_JUNIT_XML = b"""<?xml version="1.0"?>
<testsuite name="pytest"><testcase classname="posthog.test_foo.TestThing" name="test_one" time="0.5"/></testsuite>
"""
+# What pytest writes for a product that has no tests yet: valid, parseable, and empty.
+_NO_TESTS_JUNIT_XML = b"""<?xml version="1.0"?>
+<testsuites name="pytest tests"><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="0" time="0.479"/></testsuites>
+"""
+
class TestPickOutlier:
def test_all_same_returns_value(self):
@@ -530,6 +535,50 @@ def test_fail_on_drift_refuses_an_incomplete_junit_set(tmp_path: Path, monkeypat
assert not out.exists()
+def test_fail_on_drift_accepts_a_shard_whose_junit_ran_no_tests(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ # A product with no tests yet uploads a valid JUnit declaring tests="0". Reading that
+ # as a missing clock made the strict run exit, and the workflow then kept the previous
+ # products slice — freezing the timings of every product in the run, not just the empty
+ # one. The slice must be written, and keep the shard's entries.
+ artifacts = tmp_path / "timing_artifacts"
+ timings = {
+ "1": {"posthog/test_foo.py::TestThing::test_one": 0.5},
+ "2": {"posthog/test_bar.py::TestOther::test_two": 1.0},
+ }
+ for shard, entries in timings.items():
+ shard_dir = artifacts / f"timing_data-Core-{shard}"
+ shard_dir.mkdir(parents=True)
+ (shard_dir / ".test_durations").write_text(json.dumps(entries))
+ junit_dir = tmp_path / "junit_artifacts"
+ for shard, xml in (("1", _MIN_JUNIT_XML), ("2", _NO_TESTS_JUNIT_XML)):
+ (junit_dir / f"junit-results-backend-core-{shard}").mkdir(parents=True)
+ (junit_dir / f"junit-results-backend-core-{shard}" / "junit.xml").write_bytes(xml)
+ out = tmp_path / "core_durations"
+ monkeypatch.setattr(
+ sys,
+ "argv",
+ [
+ "optimize_test_durations.py",
+ str(artifacts),
+ str(out),
+ "--segment",
+ "Core",
+ "--junit-dir",
+ str(junit_dir),
+ "--fail-on-drift",
+ ],
+ )
+
+ main()
+
+ assert json.loads(out.read_text()) == {
+ "posthog/test_foo.py::TestThing::test_one": 0.5,
+ "posthog/test_bar.py::TestOther::test_two": 1.0,
+ }
+
+
@pytest.mark.parametrize(
"mapped_seconds, drifts",
[