Sub topic 2
JUnit XML: the common test report format
This chapter assumes you can read basic XML and run a test suite from the command line. It also assumes you know the workflow syntax from earlier chapters. It explains the file format that later chapters read, upload, and summarize.
Why JUnit XML
JUnit is a Java testing framework. Its XML report format became the shared way for test tools to describe results. Today pytest, Maven, Gradle, Jest (through a plugin), Go (through converters), and many others can write it. CI systems and reporting actions read it. If your runner can write JUnit XML, one parser can handle results from many languages.
The format is a de facto convention. There is no single official schema, so tools differ in small details. Treat each runner's output as the authority.
The structure of a report
A report is a tree with three levels.
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<testsuite name="pytest" tests="3" failures="1" errors="0" skipped="1" time="0.120">
<testcase classname="tests.test_math" name="test_add" time="0.001"/>
<testcase classname="tests.test_math" name="test_div" time="0.003">
<failure message="assert 3 == 4">def test_div():
> assert 1 + 2 == 4
E assert 3 == 4</failure>
</testcase>
<testcase classname="tests.test_math" name="test_slow" time="0.000">
<skipped message="needs network"/>
</testcase>
</testsuite>
</testsuites>The elements work like this:
| Element | Meaning |
|---|---|
| `testsuites` | Optional root that holds one or more suites. Some tools omit it and use `testsuite` as the root. |
| `testsuite` | A group of tests, with counts in its attributes. |
| `testcase` | One test. A test with no child element passed. |
| `failure` | An assertion did not hold. |
| `error` | The test could not complete, for example an exception in a fixture or setup. |
| `skipped` | The test was not run. |
| `system-out`, `system-err` | Captured output, when the runner includes it. |
The most useful attributes are tests, failures, errors, skipped, and time (seconds) on the suite. On each test case they are classname, name, and time.
Failures versus errors
A failure means the test ran and a check was false. An error means something unexpected broke before or during the test. Both should fail your build, but they point to different problems. A failure usually means the code under test is wrong. An error often means the test or its setup is wrong.
Reading the report in a script
Because the file is plain XML, the standard library is enough to inspect it. This script prints each failed test with its message.
import sys
import xml.etree.ElementTree as ET
root = ET.parse(sys.argv[1]).getroot()
for case in root.iter("testcase"):
problem = case.find("failure")
if problem is None:
problem = case.find("error")
if problem is not None:
name = f"{case.get('classname')}.{case.get('name')}"
print(f"{name}: {problem.get('message')}")root.iter(...) searches the whole tree, so the script works whether the root is testsuites or testsuite. Use the same approach when you total counts. Summing the attributes of every testsuite works for flat output, but nested suites would count some tests twice.
Common problems
- Empty or missing file. If the runner crashes before it writes the report, the file may not exist. Later steps that read it will fail with a confusing "file not found" error.
- Counts that disagree between tools. Some runners count errors inside
failures, and others keep them separate. Check your runner before you compare numbers. - Duplicate test names. Reports from matrix jobs can contain identical
classnameandnamevalues. Include the matrix value in the report file name so you can tell them apart. - Special characters. Test output may contain characters that are not valid in XML. Good runners escape them, but hand-written generators often do not.
Where this fits
The next chapter shows how to make your runner write this file. After that, you will keep it when tests fail and upload it as an artifact.
Exercise
A report has <testsuite tests="10" failures="1" errors="2" skipped="1">. How many tests passed, assuming failures, errors, and skipped are counted separately?
Show answer
Six tests passed. Subtract the failures (1), errors (2), and skipped tests (1) from the total of 10.
Exercise
A fixture raises an exception before a test body runs. Which child element should the report contain for that test case, and why?
Show answer
An error element. The test could not complete, which is different from a failure, where the test ran and an assertion was false.