def test_fromstring_no_testsuites(self): text = """<testsuite name="suitename1"> <testcase name="testname1"> </testcase></testsuite>""" result = JUnitXml.fromstring(text) self.assertEqual(result.time, 0) self.assertEqual(len(result), 1)
def summarise_junit(file_path: str) -> bool: """Parse jUnit output and show a summary. Preprocesses input to increase the chances of getting valid XML. Returns True if there were no failures or errors, raises exception on IOError or XML parse errors.""" with open(file_path, 'r') as file: # Skip log text before and after the XML, escape some of the test output to get valid XML lines = StringIO() take = False for line in file: if line.strip() == '<testsuite>': take = True if take: lines.write(line.replace('<<', '<<')) if line.strip() == "</testsuite>": break xml = JUnitXml.fromstring(strip_ansi(lines.getvalue())) print("") print("Test summary") print("------------") print(f"tests: {xml.tests}") print(f"skipped: {xml.skipped}") print(f"failures: {xml.failures}") print(f"errors: {xml.errors}\n") return xml.failures == 0 and xml.errors == 0
def test_fromstring(self): text = """<testsuites><testsuite name="suitename1"> <testcase name="testname1"> </testcase></testsuite> <testsuite name="suitename2"> <testcase name="testname2"> </testcase></testsuite></testsuites>""" result = JUnitXml.fromstring(text) self.assertEqual(len(result), 2)
def test_multi_results_in_case(self): # Has to be a binary string to include xml declarations. text = b"""<?xml version="1.0" encoding="UTF-8"?> <testsuites> <testsuite name="JUnitXmlReporter.constructor"> <testcase classname="JUnitXmlReporter.constructor" name="should default path to an empty string" time="0.006"> <failure message="test failure">Assertion failed</failure> <skipped /> </testcase> </testsuite> </testsuites>""" xml = JUnitXml.fromstring(text) suite = next(iter(xml)) case = next(iter(suite)) self.assertEqual(len(case.result), 2)
def test_fromstring_multiple_fails(self): text = """<testsuites> <testsuite errors="1" failures="0" hostname="hooch" name="pytest" skipped="1" tests="3" time="0.025" timestamp="2020-02-05T10:52:33.843536"> <testcase classname="test_x" file="test_x.py" line="7" name="test_comp_1" time="1""" + ( "," if has_us_locale else "") + """000.000"/> <testcase classname="test_x" file="test_x.py" line="10" name="test_comp_2" time="0.000"> <skipped message="unconditional skip" type="pytest.skip">test_x.py:11: unconditional skip</skipped> <error message="test teardown failure"> @pytest.fixture(scope="module") def compb(): yield > raise PermissionError E PermissionError test_x.py:6: PermissionError </error> </testcase> </testsuite> </testsuites>""" result = JUnitXml.fromstring(text) self.assertEqual(result.errors, 1) self.assertEqual(result.skipped, 1) suite = list(iter(result))[0] cases = list(iter(suite)) self.assertEqual(len(cases[0].result), 0) self.assertEqual(len(cases[1].result), 2) text = cases[1].result[1].text self.assertTrue("@pytest.fixture" in text)
def test_fromstring_invalid(self): text = """<random name="suitename1"></random>""" with self.assertRaises(Exception) as context: JUnitXml.fromstring(text) self.assertTrue(isinstance(context.exception, JUnitXmlError))