Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/5477.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The XML file produced by ``--junitxml`` now correctly contain a ``<testsuites>`` root element.
21 changes: 10 additions & 11 deletions src/_pytest/junitxml.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this public API? If so we should note the change somewhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it is used only on the test. 👍

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