Skip to content
Latchkey

How to Split Tests by Timing Data From JUnit Reports

JUnit XML records each test time attribute, which you can use to pack tests into shards of roughly equal total duration.

Parse the time attribute from prior JUnit reports, then greedily assign each test to the currently lightest shard.

Steps

  • Produce a JUnit XML report on each run and store it as an artifact.
  • Parse <testcase time="..."> values from the last known-good report.
  • Greedily assign tests to the shard with the smallest running total.

Terminal

Terminal
# extract test name + duration from JUnit XML
python - <<'PY'
import xml.etree.ElementTree as ET
tree = ET.parse('junit.xml')
for tc in tree.iter('testcase'):
    print(tc.attrib['classname'], tc.attrib['name'], tc.attrib['time'])
PY

Gotchas

  • Greedy longest-processing-time-first assignment balances better than round-robin.
  • A missing report on first run means you must fall back to file-count splitting.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →