Skip to content

Commit 2095372

Browse files
committed
More tests on writing
1 parent 966f8a6 commit 2095372

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

tests/test_fromfile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from unittest import skipIf
66
from junitparser import (
77
TestCase,
8+
TestSuite,
89
Skipped,
910
Failure,
1011
JUnitXmlError,

tests/test_general.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def test_fromstring(self):
124124
<testcase name="testname2">
125125
</testcase></testsuite></testsuites>"""
126126
result = JUnitXml.fromstring(text)
127+
assert isinstance(result, JUnitXml)
127128
assert len(result) == 2
128129
assert result.time == 0
129130

@@ -132,6 +133,7 @@ def test_fromstring_no_testsuites(self):
132133
<testcase name="testname1">
133134
</testcase></testsuite>"""
134135
result = JUnitXml.fromstring(text)
136+
assert isinstance(result, JUnitXml)
135137
assert len(result) == 1
136138
assert result.time == 0
137139

tests/test_write.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,43 @@ def test_write_nonascii():
129129
assert xmlfile.getvalue().decode("utf-8") == get_expected_xml("用例1")
130130

131131

132+
def test_write_no_testsuites():
133+
# Has to be a binary string to include xml declarations.
134+
text = b"""<?xml version="1.0" encoding="UTF-8"?>
135+
<testsuite name="JUnitXmlReporter.constructor">
136+
<testcase classname="JUnitXmlReporter.constructor" name="should default path to an empty string" time="0.006"/>
137+
</testsuite>"""
138+
xml = JUnitXml.fromstring(text)
139+
assert isinstance(xml, JUnitXml)
140+
suite = next(iter(xml))
141+
assert isinstance(suite, TestSuite)
142+
case = next(iter(suite))
143+
assert isinstance(case, TestCase)
144+
assert len(case.result) == 0
145+
146+
# writing this JUnitXml object contains a root <testsuites> element
147+
xmlfile = BytesIO()
148+
xml.write(xmlfile)
149+
assert xmlfile.getvalue().decode("utf-8") == (
150+
"<?xml version='1.0' encoding='UTF-8'?>\n"
151+
'<testsuites><testsuite name="JUnitXmlReporter.constructor">\n'
152+
' <testcase classname="JUnitXmlReporter.constructor" name="should default '
153+
'path to an empty string" time="0.006"/>\n'
154+
'</testsuite></testsuites>'
155+
)
156+
157+
# writing the inner testsuite reproduces the input string
158+
xmlfile = BytesIO()
159+
suite.write(xmlfile)
160+
assert xmlfile.getvalue().decode("utf-8") == (
161+
"<?xml version='1.0' encoding='UTF-8'?>\n"
162+
'<testsuite name="JUnitXmlReporter.constructor">\n'
163+
' <testcase classname="JUnitXmlReporter.constructor" name="should default '
164+
'path to an empty string" time="0.006"/>\n'
165+
'</testsuite>'
166+
)
167+
168+
132169
def test_read_written_xml():
133170
suite1 = TestSuite()
134171
suite1.name = "suite1"

0 commit comments

Comments
 (0)