Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JUnitXML.fromroot #92

Merged
merged 1 commit into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions junitparser/junitparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,8 @@ def update_statistics(self):
self.time = round(time, 3)

@classmethod
def fromstring(cls, text):
"""Construct Junit objects from a XML string."""
root_elem = etree.fromstring(text) # nosec
def fromroot(cls, root_elem):
"""Constructs Junit objects from an elementTree root element."""
if root_elem.tag == "testsuites":
instance = cls()
elif root_elem.tag == "testsuite":
Expand All @@ -305,6 +304,12 @@ def fromstring(cls, text):
instance._elem = root_elem
return instance

@classmethod
def fromstring(cls, text):
"""Construct Junit objects from a XML string."""
root_elem = etree.fromstring(text) # nosec
return cls.fromroot(root_elem)

@classmethod
def fromfile(cls, filepath, parse_func=None):
"""Initiate the object from a report file."""
Expand All @@ -313,13 +318,7 @@ def fromfile(cls, filepath, parse_func=None):
else:
tree = etree.parse(filepath) # nosec
root_elem = tree.getroot()
if root_elem.tag == "testsuites":
instance = cls()
elif root_elem.tag == "testsuite":
instance = TestSuite()
else:
raise JUnitXmlError("Invalid format.")
instance._elem = root_elem
instance = cls.fromroot(root_elem)
instance.filepath = filepath
return instance

Expand Down
46 changes: 46 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,52 @@ def test_fromstring_multiple_fails(self):
</testsuite>
</testsuites>"""
result = JUnitXml.fromstring(text)
self.assertIsInstance(result, JUnitXml)
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_fromroot_testsuite(self):
text = """
<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="0.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>"""
root_elemt = etree.fromstring(text)
result = JUnitXml.fromroot(root_elemt)
self.assertIsInstance(result, TestSuite)
self.assertEqual(result.errors, 1)
self.assertEqual(result.skipped, 1)
cases = list(iter(result))
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_fromroot_testsuites(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="0.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>"""
root_elemt = etree.fromstring(text)
result = JUnitXml.fromroot(root_elemt)
self.assertEqual(result.errors, 1)
self.assertEqual(result.skipped, 1)
suite = list(iter(result))[0]
Expand Down