Skip to content

Commit

Permalink
Calling junitparser.xunit2.JUnitXml.fromroot should produce `junitp…
Browse files Browse the repository at this point in the history
…arser.xunit2.TestSuite` (#134)
  • Loading branch information
EnricoMi authored Aug 30, 2024
1 parent d2b7796 commit aa955b5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
4 changes: 3 additions & 1 deletion junitparser/junitparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,8 @@ class JUnitXml(Element):
errors = IntAttr()
skipped = IntAttr()

testsuite = TestSuite

def __init__(self, name=None):
super().__init__(self._tag)
self.filepath = None
Expand Down Expand Up @@ -729,7 +731,7 @@ def fromroot(cls, root_elem: Element):
if root_elem.tag == "testsuites":
instance = cls()
elif root_elem.tag == "testsuite":
instance = TestSuite()
instance = cls.testsuite()
else:
raise JUnitXmlError("Invalid format.")
instance._elem = root_elem
Expand Down
42 changes: 22 additions & 20 deletions junitparser/xunit2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,6 @@
T = TypeVar("T")


class JUnitXml(junitparser.JUnitXml):
# Pytest and xunit schema doesn't have "skipped" in testsuites
skipped = None

def update_statistics(self):
"""Update test count, time, etc."""
time = 0
tests = failures = errors = 0
for suite in self:
suite.update_statistics()
tests += suite.tests
failures += suite.failures
errors += suite.errors
time += suite.time
self.tests = tests
self.failures = failures
self.errors = errors
self.time = round(time, 3)


class TestSuite(junitparser.TestSuite):
"""TestSuite for Pytest, with some different attributes."""

Expand Down Expand Up @@ -93,6 +73,28 @@ def system_err(self, value: str):
self.append(err)


class JUnitXml(junitparser.JUnitXml):
# Pytest and xunit schema doesn't have "skipped" in testsuites
skipped = None

testsuite = TestSuite

def update_statistics(self):
"""Update test count, time, etc."""
time = 0
tests = failures = errors = 0
for suite in self:
suite.update_statistics()
tests += suite.tests
failures += suite.failures
errors += suite.errors
time += suite.time
self.tests = tests
self.failures = failures
self.errors = errors
self.time = round(time, 3)


class StackTrace(junitparser.System):
_tag = "stackTrace"

Expand Down
11 changes: 11 additions & 0 deletions tests/test_xunit2.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,14 @@ def test_init(self):
assert xml.skipped is None
assert xml.tostring().count(b"errors") == 2
assert xml.tostring().count(b"skipped") == 1

def test_fromstring(self):
text = """<testsuite name="suite name">
<testcase name="test name 1"/>
<testcase name="test name 2"/>
</testsuite>"""
suite = JUnitXml.fromstring(text)
assert isinstance(suite, TestSuite)
assert suite.name == "suite name"
assert len(list(suite)) == 2
assert [test.name for test in suite] == ["test name 1", "test name 2"]

0 comments on commit aa955b5

Please sign in to comment.