Skip to content

Commit 2dfd410

Browse files
committed
junitxml: Fix double system-out tags per testcase
In the xml report we now have two occurences for the system-out tag if the testcase writes to stdout both on call and teardown and fails in teardown. This behaviour is against the xsd. This patch makes sure that the system-out section exists only once per testcase.
1 parent ccf9877 commit 2dfd410

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
collected by the built-in python test collector. Thanks `@barneygale`_ for the
1515
PR.
1616

17+
* junitxml: Fix problematic case where system-out tag occured twice per testcase
18+
element in the XML report. Thanks `@kkoukiou` for the PR.
19+
1720
*
1821

1922
.. _#2160: https://github.com/pytest-dev/pytest/issues/2160
@@ -23,6 +26,7 @@
2326
.. _#2231: https://github.com/pytest-dev/pytest/issues/2231
2427

2528
.. _@vidartf: https://github.com/vidartf
29+
.. _@kkoukiou: https://github.com/KKoukiou
2630

2731

2832
3.0.6 (2017-01-22)

_pytest/junitxml.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def _add_simple(self, kind, message, data=None):
119119
node = kind(data, message=message)
120120
self.append(node)
121121

122-
def _write_captured_output(self, report):
122+
def write_captured_output(self, report):
123123
for capname in ('out', 'err'):
124124
content = getattr(report, 'capstd' + capname)
125125
if content:
@@ -128,7 +128,6 @@ def _write_captured_output(self, report):
128128

129129
def append_pass(self, report):
130130
self.add_stats('passed')
131-
self._write_captured_output(report)
132131

133132
def append_failure(self, report):
134133
# msg = str(report.longrepr.reprtraceback.extraline)
@@ -147,7 +146,6 @@ def append_failure(self, report):
147146
fail = Junit.failure(message=message)
148147
fail.append(bin_xml_escape(report.longrepr))
149148
self.append(fail)
150-
self._write_captured_output(report)
151149

152150
def append_collect_error(self, report):
153151
# msg = str(report.longrepr.reprtraceback.extraline)
@@ -165,7 +163,6 @@ def append_error(self, report):
165163
msg = "test setup failure"
166164
self._add_simple(
167165
Junit.error, msg, report.longrepr)
168-
self._write_captured_output(report)
169166

170167
def append_skipped(self, report):
171168
if hasattr(report, "wasxfail"):
@@ -180,7 +177,7 @@ def append_skipped(self, report):
180177
Junit.skipped("%s:%s: %s" % (filename, lineno, skipreason),
181178
type="pytest.skip",
182179
message=skipreason))
183-
self._write_captured_output(report)
180+
self.write_captured_output(report)
184181

185182
def finalize(self):
186183
data = self.to_xml().unicode(indent=0)
@@ -345,6 +342,8 @@ def pytest_runtest_logreport(self, report):
345342
reporter.append_skipped(report)
346343
self.update_testcase_duration(report)
347344
if report.when == "teardown":
345+
reporter = self._opentestcase(report)
346+
reporter.write_captured_output(report)
348347
self.finalize(report)
349348

350349
def update_testcase_duration(self, report):

testing/test_junitxml.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,25 @@ def test_function(arg):
557557
systemout = pnode.find_first_by_tag("system-err")
558558
assert "hello-stderr" in systemout.toxml()
559559

560+
def test_avoid_double_stdout(self, testdir):
561+
testdir.makepyfile("""
562+
import sys
563+
import pytest
564+
565+
@pytest.fixture
566+
def arg(request):
567+
yield
568+
sys.stdout.write('hello-stdout teardown')
569+
raise ValueError()
570+
def test_function(arg):
571+
sys.stdout.write('hello-stdout call')
572+
""")
573+
result, dom = runandparse(testdir)
574+
node = dom.find_first_by_tag("testsuite")
575+
pnode = node.find_first_by_tag("testcase")
576+
systemout = pnode.find_first_by_tag("system-out")
577+
assert "hello-stdout call" in systemout.toxml()
578+
assert "hello-stdout teardown" in systemout.toxml()
560579

561580
def test_mangle_test_address():
562581
from _pytest.junitxml import mangle_test_address

0 commit comments

Comments
 (0)