Skip to content

Commit

Permalink
More tests on writing
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoMi committed Jan 29, 2025
1 parent 966f8a6 commit 2095372
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/test_fromfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from unittest import skipIf
from junitparser import (
TestCase,
TestSuite,
Skipped,
Failure,
JUnitXmlError,
Expand Down
2 changes: 2 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def test_fromstring(self):
<testcase name="testname2">
</testcase></testsuite></testsuites>"""
result = JUnitXml.fromstring(text)
assert isinstance(result, JUnitXml)
assert len(result) == 2
assert result.time == 0

Expand All @@ -132,6 +133,7 @@ def test_fromstring_no_testsuites(self):
<testcase name="testname1">
</testcase></testsuite>"""
result = JUnitXml.fromstring(text)
assert isinstance(result, JUnitXml)
assert len(result) == 1
assert result.time == 0

Expand Down
37 changes: 37 additions & 0 deletions tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,43 @@ def test_write_nonascii():
assert xmlfile.getvalue().decode("utf-8") == get_expected_xml("用例1")


def test_write_no_testsuites():
# Has to be a binary string to include xml declarations.
text = b"""<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="JUnitXmlReporter.constructor">
<testcase classname="JUnitXmlReporter.constructor" name="should default path to an empty string" time="0.006"/>
</testsuite>"""
xml = JUnitXml.fromstring(text)
assert isinstance(xml, JUnitXml)
suite = next(iter(xml))
assert isinstance(suite, TestSuite)
case = next(iter(suite))
assert isinstance(case, TestCase)
assert len(case.result) == 0

# writing this JUnitXml object contains a root <testsuites> element
xmlfile = BytesIO()
xml.write(xmlfile)
assert xmlfile.getvalue().decode("utf-8") == (
"<?xml version='1.0' encoding='UTF-8'?>\n"
'<testsuites><testsuite name="JUnitXmlReporter.constructor">\n'
' <testcase classname="JUnitXmlReporter.constructor" name="should default '
'path to an empty string" time="0.006"/>\n'
'</testsuite></testsuites>'
)

# writing the inner testsuite reproduces the input string
xmlfile = BytesIO()
suite.write(xmlfile)
assert xmlfile.getvalue().decode("utf-8") == (
"<?xml version='1.0' encoding='UTF-8'?>\n"
'<testsuite name="JUnitXmlReporter.constructor">\n'
' <testcase classname="JUnitXmlReporter.constructor" name="should default '
'path to an empty string" time="0.006"/>\n'
'</testsuite>'
)


def test_read_written_xml():
suite1 = TestSuite()
suite1.name = "suite1"
Expand Down

0 comments on commit 2095372

Please sign in to comment.