xref: /linux/tools/testing/kunit/kunit_junit.py (revision 0eaed89c18aeedf0898baf2dbf5ff027c6795152)
1*e9e05c72SDavid Gow# SPDX-License-Identifier: GPL-2.0
2*e9e05c72SDavid Gow#
3*e9e05c72SDavid Gow# Generates JUnit XML files from KUnit test results
4*e9e05c72SDavid Gow#
5*e9e05c72SDavid Gow# Copyright (C) 2026, Google LLC and David Gow.
6*e9e05c72SDavid Gow
7*e9e05c72SDavid Gowfrom xml.sax.saxutils import quoteattr, XMLGenerator
8*e9e05c72SDavid Gowimport xml.etree.ElementTree as ET
9*e9e05c72SDavid Gowfrom kunit_parser import Test, TestStatus
10*e9e05c72SDavid Gowfrom typing import Optional
11*e9e05c72SDavid Gow
12*e9e05c72SDavid Gow# Get a string representing a tes suite (including subtests) in JUnit XML
13*e9e05c72SDavid Gowdef get_test_suite(test: Test, parent: Optional[ET.Element]) -> ET.Element:
14*e9e05c72SDavid Gow	suite_attrs = {
15*e9e05c72SDavid Gow		'name': test.name,
16*e9e05c72SDavid Gow		'tests': str(test.counts.total()),
17*e9e05c72SDavid Gow		'failures': str(test.counts.failed),
18*e9e05c72SDavid Gow		'skipped': str(test.counts.skipped),
19*e9e05c72SDavid Gow		'errors': str(test.counts.crashed + test.counts.errors),
20*e9e05c72SDavid Gow	}
21*e9e05c72SDavid Gow
22*e9e05c72SDavid Gow	if parent is not None:
23*e9e05c72SDavid Gow		test_suite_element = ET.SubElement(parent, 'testsuite', suite_attrs)
24*e9e05c72SDavid Gow	else:
25*e9e05c72SDavid Gow		test_suite_element = ET.Element('testsuite', suite_attrs)
26*e9e05c72SDavid Gow
27*e9e05c72SDavid Gow	for subtest in test.subtests:
28*e9e05c72SDavid Gow		if subtest.subtests:
29*e9e05c72SDavid Gow			get_test_suite(subtest, test_suite_element)
30*e9e05c72SDavid Gow			continue
31*e9e05c72SDavid Gow		test_case_element = ET.SubElement(test_suite_element, 'testcase', {'name': subtest.name})
32*e9e05c72SDavid Gow		if subtest.status == TestStatus.FAILURE:
33*e9e05c72SDavid Gow			ET.SubElement(test_case_element, 'failure', {}).text = 'Test Failed'
34*e9e05c72SDavid Gow		elif subtest.status == TestStatus.SKIPPED:
35*e9e05c72SDavid Gow			ET.SubElement(test_case_element, 'skipped', {}).text = subtest.skip_reason
36*e9e05c72SDavid Gow		elif subtest.status == TestStatus.TEST_CRASHED:
37*e9e05c72SDavid Gow			ET.SubElement(test_case_element, 'error', {}).text = 'Test Crashed'
38*e9e05c72SDavid Gow
39*e9e05c72SDavid Gow		if subtest.log:
40*e9e05c72SDavid Gow			ET.SubElement(test_case_element, 'system-out', {}).text = "\n".join(subtest.log)
41*e9e05c72SDavid Gow
42*e9e05c72SDavid Gow	return test_suite_element
43*e9e05c72SDavid Gow
44*e9e05c72SDavid Gow# Get a string for an entire XML file for the test structure starting at test
45*e9e05c72SDavid Gowdef get_junit_result(test: Test) -> str:
46*e9e05c72SDavid Gow	root_element = get_test_suite(test, None)
47*e9e05c72SDavid Gow	ET.indent(root_element)
48*e9e05c72SDavid Gow	return ET.tostring(root_element, encoding="unicode", xml_declaration=True)
49*e9e05c72SDavid Gow
50*e9e05c72SDavid Gow# Print a JUnit result to stdout.
51*e9e05c72SDavid Gowdef print_junit_result(test: Test) -> None:
52*e9e05c72SDavid Gow	root_element = get_test_suite(test, None)
53*e9e05c72SDavid Gow	ET.indent(root_element)
54*e9e05c72SDavid Gow	ET.dump(root_element)
55*e9e05c72SDavid Gow
56*e9e05c72SDavid Gow# Write an entire XML file for the test structure starting at test
57*e9e05c72SDavid Gowdef write_junit_result(test: Test, filename: str) -> None:
58*e9e05c72SDavid Gow	root_element = get_test_suite(test, None)
59*e9e05c72SDavid Gow	ET.indent(root_element)
60*e9e05c72SDavid Gow	root_et = ET.ElementTree(root_element)
61*e9e05c72SDavid Gow	root_et.write(filename, encoding='utf-8', xml_declaration=True)
62