From 41f57ef95db48be04faea33dd5bd2b414f42a395 Mon Sep 17 00:00:00 2001 From: Alex <59836767+acjreno@users.noreply.github.com> Date: Sun, 16 Apr 2023 16:31:45 -0400 Subject: [PATCH] Fix `pytrace=False` and `--tb=line` reports `None` (#10905) Closes #10831. This fixes a small bug where running tests that contained `pytest.fail(pytrace=False)` with the `--tb=line` flag set results in an output of "None" in the Failures section of the output, and adds a test to ensure the behavior is correct. --- AUTHORS | 1 + changelog/10831.bugfix.rst | 1 + src/_pytest/_code/code.py | 6 ++---- testing/test_terminal.py | 13 +++++++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 changelog/10831.bugfix.rst diff --git a/AUTHORS b/AUTHORS index 1aa5265e6..dee73514a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -13,6 +13,7 @@ Ahn Ki-Wook Akiomi Kamakura Alan Velasco Alessio Izzo +Alex Jones Alexander Johnson Alexander King Alexei Kozlenok diff --git a/changelog/10831.bugfix.rst b/changelog/10831.bugfix.rst new file mode 100644 index 000000000..ea641dee6 --- /dev/null +++ b/changelog/10831.bugfix.rst @@ -0,0 +1 @@ +Terminal Reporting: Fixed bug when running in ``--tb=line`` mode where ``pytest.fail(pytrace=False)`` tests report ``None``. diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 67d1062ff..5bc78f478 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -647,7 +647,7 @@ class ExceptionInfo(Generic[E]): Ignored if ``style=="native"``. :param str style: - long|short|no|native|value traceback style. + long|short|line|no|native|value traceback style. :param bool abspath: If paths should be changed to absolute or left unchanged. @@ -977,9 +977,7 @@ class FormattedExcinfo: ) else: reprtraceback = self.repr_traceback(excinfo_) - reprcrash: Optional[ReprFileLocation] = ( - excinfo_._getreprcrash() if self.style != "value" else None - ) + reprcrash = excinfo_._getreprcrash() else: # Fallback to native repr if the exception doesn't have a traceback: # ExceptionInfo objects require a full traceback to work. diff --git a/testing/test_terminal.py b/testing/test_terminal.py index fe325b72d..97ce2cb56 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -1539,6 +1539,19 @@ class TestGenericReporting: s = result.stdout.str() assert "def test_func2" not in s + def test_tb_crashline_pytrace_false(self, pytester: Pytester, option) -> None: + p = pytester.makepyfile( + """ + import pytest + def test_func1(): + pytest.fail('test_func1', pytrace=False) + """ + ) + result = pytester.runpytest("--tb=line") + result.stdout.str() + bn = p.name + result.stdout.fnmatch_lines(["*%s:3: Failed: test_func1" % bn]) + def test_pytest_report_header(self, pytester: Pytester, option) -> None: pytester.makeconftest( """