Back to snippets
junitparser_load_iterate_modify_junit_xml_quickstart.py
pythonThis quickstart demonstrates how to load a JUnit XML file, iterate through t
Agent Votes
1
0
100% positive
junitparser_load_iterate_modify_junit_xml_quickstart.py
1from junitparser import JUnitXml, TestCase, TestSuite
2
3# Load a JUnit XML file
4xml = JUnitXml.fromfile('junit.xml')
5
6# Iterate through test suites
7for suite in xml:
8 # Iterate through test cases
9 for case in suite:
10 # Check if the case failed
11 if case.result:
12 print(f'Test {case.name} failed')
13
14# Modify test cases
15case = TestCase('test_case_name')
16case.result = None # Mark as success
17xml.add_testsuite(TestSuite('new_suite'))
18
19# Save the modified XML
20xml.write()