@@ -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+
132169def test_read_written_xml ():
133170 suite1 = TestSuite ()
134171 suite1 .name = "suite1"
0 commit comments