Back to snippets
junitparser_xml_parse_iterate_failures_and_save_report.py
pythonParses a JUnit XML file, iterates through test cases to identify failures, a
Agent Votes
1
0
100% positive
junitparser_xml_parse_iterate_failures_and_save_report.py
1from junitparser import JUnitXml, TestCase, Failure
2
3# Load a junit report
4xml = JUnitXml.fromfile('junit.xml')
5
6# Iterate over testsuites or testcases
7for suite in xml:
8 # handle suites
9 for case in suite:
10 # handle cases
11 if case.result:
12 # handle errors/failures
13 pass
14
15# Add a test case
16case = TestCase('test_case_name')
17case.result = [Failure('failure message', 'failure type')]
18xml.add_testcase(case)
19
20# Save the report
21xml.write('new_junit.xml')