Skip to content

junitxml: Fix double system-out tags per testcase #2271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2017
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
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
3.0.7 (unreleased)
==================

* junitxml: Fix problematic case where system-out tag occured twice per testcase
element in the XML report. Thanks `@kkoukiou`_ for the PR.

* Fix regression, pytest now skips unittest correctly if run with ``--pdb``
(`#2137`_). Thanks to `@gst`_ for the report and `@mbyt`_ for the PR.

Expand Down Expand Up @@ -31,6 +34,7 @@
.. _@gst: https://github.com/gst
.. _@sirex: https://github.com/sirex
.. _@vidartf: https://github.com/vidartf
.. _@kkoukiou: https://github.com/KKoukiou

.. _#2137: https://github.com/pytest-dev/pytest/issues/2137
.. _#2160: https://github.com/pytest-dev/pytest/issues/2160
Expand Down
9 changes: 4 additions & 5 deletions _pytest/junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def _add_simple(self, kind, message, data=None):
node = kind(data, message=message)
self.append(node)

def _write_captured_output(self, report):
def write_captured_output(self, report):
for capname in ('out', 'err'):
content = getattr(report, 'capstd' + capname)
if content:
Expand All @@ -128,7 +128,6 @@ def _write_captured_output(self, report):

def append_pass(self, report):
self.add_stats('passed')
self._write_captured_output(report)

def append_failure(self, report):
# msg = str(report.longrepr.reprtraceback.extraline)
Expand All @@ -147,7 +146,6 @@ def append_failure(self, report):
fail = Junit.failure(message=message)
fail.append(bin_xml_escape(report.longrepr))
self.append(fail)
self._write_captured_output(report)

def append_collect_error(self, report):
# msg = str(report.longrepr.reprtraceback.extraline)
Expand All @@ -165,7 +163,6 @@ def append_error(self, report):
msg = "test setup failure"
self._add_simple(
Junit.error, msg, report.longrepr)
self._write_captured_output(report)

def append_skipped(self, report):
if hasattr(report, "wasxfail"):
Expand All @@ -180,7 +177,7 @@ def append_skipped(self, report):
Junit.skipped("%s:%s: %s" % (filename, lineno, skipreason),
type="pytest.skip",
message=skipreason))
self._write_captured_output(report)
self.write_captured_output(report)

def finalize(self):
data = self.to_xml().unicode(indent=0)
Expand Down Expand Up @@ -345,6 +342,8 @@ def pytest_runtest_logreport(self, report):
reporter.append_skipped(report)
self.update_testcase_duration(report)
if report.when == "teardown":
reporter = self._opentestcase(report)
reporter.write_captured_output(report)
self.finalize(report)

def update_testcase_duration(self, report):
Expand Down
19 changes: 19 additions & 0 deletions testing/test_junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,25 @@ def test_function(arg):
systemout = pnode.find_first_by_tag("system-err")
assert "hello-stderr" in systemout.toxml()

def test_avoid_double_stdout(self, testdir):
testdir.makepyfile("""
import sys
import pytest

@pytest.fixture
def arg(request):
yield
sys.stdout.write('hello-stdout teardown')
raise ValueError()
def test_function(arg):
sys.stdout.write('hello-stdout call')
""")
result, dom = runandparse(testdir)
node = dom.find_first_by_tag("testsuite")
pnode = node.find_first_by_tag("testcase")
systemout = pnode.find_first_by_tag("system-out")
assert "hello-stdout call" in systemout.toxml()
assert "hello-stdout teardown" in systemout.toxml()

def test_mangle_test_address():
from _pytest.junitxml import mangle_test_address
Expand Down