From 08002ab75ad82c38c647d51969d9c63f4cae99f1 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 3 Aug 2016 21:11:19 -0300 Subject: [PATCH] Add longreprtext property to TestReport objects Related to #1790 --- _pytest/runner.py | 14 ++++++++++++++ doc/en/writing_plugins.rst | 1 + testing/test_runner.py | 26 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/_pytest/runner.py b/_pytest/runner.py index 2ec24cd53..ad3735992 100644 --- a/_pytest/runner.py +++ b/_pytest/runner.py @@ -211,6 +211,20 @@ class BaseReport(object): if name.startswith(prefix): yield prefix, content + @property + def longreprtext(self): + """ + Read-only property that returns the full string representation + of ``longrepr``. + + .. versionadded:: 3.0 + """ + tw = py.io.TerminalWriter(stringio=True) + tw.hasmarkup = False + self.toterminal(tw) + exc = tw.stringio.getvalue() + return exc.strip() + passed = property(lambda x: x.outcome == "passed") failed = property(lambda x: x.outcome == "failed") skipped = property(lambda x: x.outcome == "skipped") diff --git a/doc/en/writing_plugins.rst b/doc/en/writing_plugins.rst index af47bad09..74533e273 100644 --- a/doc/en/writing_plugins.rst +++ b/doc/en/writing_plugins.rst @@ -632,6 +632,7 @@ Reference of objects involved in hooks .. autoclass:: _pytest.runner.TestReport() :members: + :inherited-members: .. autoclass:: _pytest.vendored_packages.pluggy._CallOutcome() :members: diff --git a/testing/test_runner.py b/testing/test_runner.py index 49379e439..a0fabb482 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -668,3 +668,29 @@ def test_store_except_info_on_eror(): assert sys.last_type is IndexError assert sys.last_value.args[0] == 'TEST' assert sys.last_traceback + + +class TestReportContents: + """ + Test ``longreprtext`` property of TestReport objects. + """ + + def test_pass(self, testdir): + reports = testdir.runitem(""" + def test_func(): + pass + """) + rep = reports[1] + assert rep.longreprtext == '' + + def test_failure(self, testdir): + reports = testdir.runitem(""" + def test_func(): + x = 1 + assert x == 4 + """) + rep = reports[1] + assert 'assert 1 == 4' in rep.longreprtext + + def getrunner(self): + return lambda item: runner.runtestprotocol(item, log=False)