Skip to content

Commit

Permalink
Include <testsuites> root tag in generated XML
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Jul 3, 2019
1 parent 4f9bf02 commit 7bef003
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions changelog/5477.bugfix.rst
@@ -0,0 +1 @@
The XML file produced by ``--junitxml`` now correctly contains a ``<testsuites>`` root element.
21 changes: 10 additions & 11 deletions src/_pytest/junitxml.py
Expand Up @@ -657,18 +657,17 @@ def pytest_sessionfinish(self):
)
logfile.write('<?xml version="1.0" encoding="utf-8"?>')

logfile.write(
Junit.testsuite(
self._get_global_properties_node(),
[x.to_xml() for x in self.node_reporters_ordered],
name=self.suite_name,
errors=self.stats["error"],
failures=self.stats["failure"],
skipped=self.stats["skipped"],
tests=numtests,
time="%.3f" % suite_time_delta,
).unicode(indent=0)
suite_node = Junit.testsuite(
self._get_global_properties_node(),
[x.to_xml() for x in self.node_reporters_ordered],
name=self.suite_name,
errors=self.stats["error"],
failures=self.stats["failure"],
skipped=self.stats["skipped"],
tests=numtests,
time="%.3f" % suite_time_delta,
)
logfile.write(Junit.testsuites([suite_node]).unicode(indent=0))
logfile.close()

def pytest_terminal_summary(self, terminalreporter):
Expand Down
30 changes: 27 additions & 3 deletions testing/test_junitxml.py
Expand Up @@ -41,6 +41,16 @@ def find_first_by_tag(self, tag):
def _by_tag(self, tag):
return self.__node.getElementsByTagName(tag)

@property
def children(self):
return [type(self)(x) for x in self.__node.childNodes]

@property
def get_unique_child(self):
children = self.children
assert len(children) == 1
return children[0]

def find_nth_by_tag(self, tag, n):
items = self._by_tag(tag)
try:
Expand Down Expand Up @@ -75,7 +85,7 @@ def tag(self):
return self.__node.tagName

@property
def next_siebling(self):
def next_sibling(self):
return type(self)(self.__node.nextSibling)


Expand Down Expand Up @@ -384,11 +394,11 @@ def test_fail():
fnode = tnode.find_first_by_tag("failure")
fnode.assert_attr(message="ValueError: 42")
assert "ValueError" in fnode.toxml()
systemout = fnode.next_siebling
systemout = fnode.next_sibling
assert systemout.tag == "system-out"
assert "hello-stdout" in systemout.toxml()
assert "info msg" not in systemout.toxml()
systemerr = systemout.next_siebling
systemerr = systemout.next_sibling
assert systemerr.tag == "system-err"
assert "hello-stderr" in systemerr.toxml()
assert "info msg" not in systemerr.toxml()
Expand Down Expand Up @@ -1094,6 +1104,20 @@ def test_x(i):
assert failed == ["test_x[22]"]


def test_root_testsuites_tag(testdir):
testdir.makepyfile(
"""
def test_x():
pass
"""
)
_, dom = runandparse(testdir)
root = dom.get_unique_child
assert root.tag == "testsuites"
suite_node = root.get_unique_child
assert suite_node.tag == "testsuite"


def test_runs_twice(testdir):
f = testdir.makepyfile(
"""
Expand Down

0 comments on commit 7bef003

Please sign in to comment.