What is JUnit XML?
JUnit XML is an output format used by software testing frameworks. While most frameworks also produce plain text output meant for a human reader, the XML output allows other tools to process this data for report generation, running Continuous Integration (CI) systems, and diagnosing larger problems.
What does JUnit XML look like?
The format has an XML element for each test which produced it, called a testcase
. These test cases are grouped into testsuite
elements, which are then grouped together into a single testsuites
element.
The output format closely matches the tests which produce it. For example, this Javascript code tested with Jest:
1describe("Day of the Week Tests", () => {2 test("today is Mon || Tues || Wed", () => {3 const today = moment().format("dddd");4 expect(["Monday", "Tuesday", "Wednesday"]).toContain(today);5 });67 test("today is Thursday or Friday", () => {8 const today = moment().format("dddd");9 expect(["Thursday", "Friday"]).toContain(today);10 });1112 test("today is Saturday or Sunday", () => {13 const today = moment().format("dddd");14 expect(["Saturday", "Sunday"]).toContain(today);15 });16});
will produce output that looks like this:
1<?xml version="1.0" encoding="UTF-8"?>23<testsuites name="jest tests" tests="3" failures="2" errors="0" time="0.288">4 <testsuite name="Day of the Week Tests" errors="0" failures="2" skipped="0" timestamp="2024-04-24T18:01:22" time="0.224" tests="3">5 <testcase classname="Day of the Week Tests today is Mon || Tues || Wed" name="Day of the Week Tests today is Mon || Tues || Wed" time="0.004">6 </testcase>7 <testcase classname="Day of the Week Tests today is Thursday or Friday" name="Day of the Week Tests today is Thursday or Friday" time="0.003">8 <failure>Error: expect(received).toContain(expected) // indexOf9Expected value: "Wednesday"10Received array: ["Thursday", "Friday"]11 at Object.toContain (/Users/josh/WebstormProjects/flake-factory/javascript/tests/jest/__tests__/jest.js:11:36)12....
Notice that the name
attributes match the description arguments to the describe
and test
functions in the test code. Depending on the exact configuration, the XML output can also include the error count, time taken to run the tests, the package where the error occurred, the actual error outputs, and the filename of the file containing the test.
What is JUnit?
JUnit is a popular testing framework for Java. One of its notable features is its easy to use XML output. JUnit was one of the earliest popular unit testing frameworks and over the years the output format has been adopted by many other users outside of the Java ecosystem, becoming a defacto standard for test output, now known as JUnit XML. While there is no official standard, JUnit XML it is widely used and broadly interoperable. This page is a good guide to the details of the “standard” as it exists in the real world.
Enabling XML Output
Most testing frameworks can produce JUnit XML, but the process of enabling it varies from framework to framework.
Many test frameworks allow you to add additional fields and elements to the output that can be used for additional information only understood by certain tools and frameworks. Most tools will ignore extra elements they don’t understand. This can be helpful when customizing a development pipeline.
Summary
JUnit XML is a common format for test output produced and consumed by most testing tools and frameworks. It enables tool interoperability and integration.