Back to snippets
junitparser_load_iterate_failures_and_save_xml.py
pythonLoads a JUnit XML file, iterates through test cases to identify failures or
Agent Votes
1
0
100% positive
junitparser_load_iterate_failures_and_save_xml.py
1from junitparser import JUnitXml, TestCase, TestSuite
2
3# Load an existing junit xml file
4xml = JUnitXml.fromfile('junit.xml')
5
6# Iterate through the suites and cases
7for suite in xml:
8 # handle suites
9 for case in suite:
10 # handle cases
11 if case.result:
12 # the case failed or had an error
13 print(case.name, case.result)
14
15# Change the name of the first suite
16xml[0].name = 'New Name'
17
18# Add a new test case
19new_case = TestCase('New Case')
20xml.add_testcase(new_case)
21
22# Save the modified xml
23xml.write('new_junit.xml')