Skip to content

Commit 08002ab

Browse files
committed
Add longreprtext property to TestReport objects
Related to #1790
1 parent 6759b04 commit 08002ab

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

_pytest/runner.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,20 @@ def get_sections(self, prefix):
211211
if name.startswith(prefix):
212212
yield prefix, content
213213

214+
@property
215+
def longreprtext(self):
216+
"""
217+
Read-only property that returns the full string representation
218+
of ``longrepr``.
219+
220+
.. versionadded:: 3.0
221+
"""
222+
tw = py.io.TerminalWriter(stringio=True)
223+
tw.hasmarkup = False
224+
self.toterminal(tw)
225+
exc = tw.stringio.getvalue()
226+
return exc.strip()
227+
214228
passed = property(lambda x: x.outcome == "passed")
215229
failed = property(lambda x: x.outcome == "failed")
216230
skipped = property(lambda x: x.outcome == "skipped")

doc/en/writing_plugins.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ Reference of objects involved in hooks
632632

633633
.. autoclass:: _pytest.runner.TestReport()
634634
:members:
635+
:inherited-members:
635636

636637
.. autoclass:: _pytest.vendored_packages.pluggy._CallOutcome()
637638
:members:

testing/test_runner.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,3 +668,29 @@ def runtest(self):
668668
assert sys.last_type is IndexError
669669
assert sys.last_value.args[0] == 'TEST'
670670
assert sys.last_traceback
671+
672+
673+
class TestReportContents:
674+
"""
675+
Test ``longreprtext`` property of TestReport objects.
676+
"""
677+
678+
def test_pass(self, testdir):
679+
reports = testdir.runitem("""
680+
def test_func():
681+
pass
682+
""")
683+
rep = reports[1]
684+
assert rep.longreprtext == ''
685+
686+
def test_failure(self, testdir):
687+
reports = testdir.runitem("""
688+
def test_func():
689+
x = 1
690+
assert x == 4
691+
""")
692+
rep = reports[1]
693+
assert 'assert 1 == 4' in rep.longreprtext
694+
695+
def getrunner(self):
696+
return lambda item: runner.runtestprotocol(item, log=False)

0 commit comments

Comments
 (0)