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'])
PYGotchas
- 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
How to Parallelize Tests by Test File in CISplit a suite across CI jobs by dividing the list of test files, using a shard index to pick a deterministic…
How to Balance Test Shards Evenly in CIKeep every CI test shard finishing at the same time by splitting on recorded duration instead of file count,…