Add capstdout and capstderr attrs to TestReport

Related to #1790
This commit is contained in:
Bruno Oliveira 2016-08-03 21:49:43 -03:00
parent 08002ab75a
commit ff296fd541
3 changed files with 63 additions and 11 deletions

View File

@ -118,13 +118,10 @@ class _NodeReporter(object):
def _write_captured_output(self, report):
for capname in ('out', 'err'):
allcontent = ""
for name, content in report.get_sections("Captured std%s" %
capname):
allcontent += content
if allcontent:
content = getattr(report, 'capstd' + capname)
if content:
tag = getattr(Junit, 'system-' + capname)
self.append(tag(bin_xml_escape(allcontent)))
self.append(tag(bin_xml_escape(content)))
def append_pass(self, report):
self.add_stats('passed')

View File

@ -225,6 +225,22 @@ class BaseReport(object):
exc = tw.stringio.getvalue()
return exc.strip()
@property
def capstdout(self):
"""Return captured text from stdout, if capturing is enabled
.. versionadded:: 3.0
"""
return ''.join(content for (prefix, content) in self.get_sections('Captured stdout'))
@property
def capstderr(self):
"""Return captured text from stderr, if capturing is enabled
.. versionadded:: 3.0
"""
return ''.join(content for (prefix, content) in self.get_sections('Captured stderr'))
passed = property(lambda x: x.outcome == "passed")
failed = property(lambda x: x.outcome == "failed")
skipped = property(lambda x: x.outcome == "skipped")

View File

@ -672,10 +672,13 @@ def test_store_except_info_on_eror():
class TestReportContents:
"""
Test ``longreprtext`` property of TestReport objects.
Test user-level API of ``TestReport`` objects.
"""
def test_pass(self, testdir):
def getrunner(self):
return lambda item: runner.runtestprotocol(item, log=False)
def test_longreprtext_pass(self, testdir):
reports = testdir.runitem("""
def test_func():
pass
@ -683,7 +686,7 @@ class TestReportContents:
rep = reports[1]
assert rep.longreprtext == ''
def test_failure(self, testdir):
def test_longreprtext_failure(self, testdir):
reports = testdir.runitem("""
def test_func():
x = 1
@ -692,5 +695,41 @@ class TestReportContents:
rep = reports[1]
assert 'assert 1 == 4' in rep.longreprtext
def getrunner(self):
return lambda item: runner.runtestprotocol(item, log=False)
def test_captured_text(self, testdir):
reports = testdir.runitem("""
import pytest
import sys
@pytest.fixture
def fix():
sys.stdout.write('setup: stdout\\n')
sys.stderr.write('setup: stderr\\n')
yield
sys.stdout.write('teardown: stdout\\n')
sys.stderr.write('teardown: stderr\\n')
assert 0
def test_func(fix):
sys.stdout.write('call: stdout\\n')
sys.stderr.write('call: stderr\\n')
assert 0
""")
setup, call, teardown = reports
assert setup.capstdout == 'setup: stdout\n'
assert call.capstdout == 'setup: stdout\ncall: stdout\n'
assert teardown.capstdout == 'setup: stdout\ncall: stdout\nteardown: stdout\n'
assert setup.capstderr == 'setup: stderr\n'
assert call.capstderr == 'setup: stderr\ncall: stderr\n'
assert teardown.capstderr == 'setup: stderr\ncall: stderr\nteardown: stderr\n'
def test_no_captured_text(self, testdir):
reports = testdir.runitem("""
def test_func():
pass
""")
rep = reports[1]
assert rep.capstdout == ''
assert rep.capstderr == ''