From 5240db6c214cf85e847a1298c3b3f7ca13c5daa1 Mon Sep 17 00:00:00 2001 From: GPery Date: Sat, 1 Feb 2020 22:16:29 +0200 Subject: [PATCH] Add --no-trace option, overrides pytrace=True for pytest.fail --- AUTHORS | 1 + changelog/6651.feature.rst | 2 ++ src/_pytest/main.py | 7 +++++++ src/_pytest/nodes.py | 4 +++- testing/test_runner.py | 22 +++++++++++++++++++--- 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 changelog/6651.feature.rst diff --git a/AUTHORS b/AUTHORS index a3200f774cf..c912b895e42 100644 --- a/AUTHORS +++ b/AUTHORS @@ -110,6 +110,7 @@ Grig Gheorghiu Grigorii Eremeev (budulianin) Guido Wesdorp Guoqiang Zhang +Guy Perry Harald Armin Massa Henk-Jaap Wagenaar Holger Kohr diff --git a/changelog/6651.feature.rst b/changelog/6651.feature.rst new file mode 100644 index 00000000000..6ec02f75bff --- /dev/null +++ b/changelog/6651.feature.rst @@ -0,0 +1,2 @@ +Add the --no-trace flag, allowing to override `pytrace=True` for `pytest.fail` calls. +This is useful to avoid automated `pytest` runs outputting unwanted information. diff --git a/src/_pytest/main.py b/src/_pytest/main.py index e5666da9fce..baa8c420f54 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -180,6 +180,13 @@ def pytest_addoption(parser): default=False, help="Don't ignore tests in a local virtualenv directory", ) + group.addoption( + "--notrace", + "--no-trace", + action="store_true", + default=False, + help="Do not output any traceback in case of failure, even when passing pytrace=True", + ) group = parser.getgroup("debugconfig", "test session debugging and configuration") group.addoption( diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 5447f254173..f2c21336594 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -287,7 +287,9 @@ def _repr_failure_py( self, excinfo: ExceptionInfo[Union[Failed, FixtureLookupError]], style=None ) -> Union[str, ReprExceptionInfo, ExceptionChainRepr, FixtureLookupErrorRepr]: if isinstance(excinfo.value, Failed): - if not excinfo.value.pytrace: + if ( + self.config is not None and self.config.getoption("notrace") + ) or not excinfo.value.pytrace: return str(excinfo.value) if isinstance(excinfo.value, FixtureLookupError): return excinfo.value.formatrepr() diff --git a/testing/test_runner.py b/testing/test_runner.py index 1600b6b7ce0..06dad20f12b 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -629,7 +629,7 @@ def pytest_sessionstart(): assert result.ret == 98 -def test_pytest_fail_notrace_runtest(testdir) -> None: +def test_pytest_fail_pytrace_false_runtest(testdir) -> None: """Test pytest.fail(..., pytrace=False) does not show tracebacks during test run.""" testdir.makepyfile( """ @@ -645,7 +645,23 @@ def teardown_function(function): result.stdout.no_fnmatch_line("*def teardown_function*") -def test_pytest_fail_notrace_collection(testdir) -> None: +def test_pytest_fail_notrace_pytrace_true_runtest(testdir) -> None: + """Test pytest.fail(..., pytrace=True) does not show tracebacks when passing --no-trace.""" + testdir.makepyfile( + """ + import pytest + def test_hello(): + pytest.fail("hello", pytrace=True) + def teardown_function(function): + pytest.fail("world", pytrace=True) + """ + ) + result = testdir.runpytest("--no-trace") + result.stdout.fnmatch_lines(["world", "hello"]) + result.stdout.no_fnmatch_line("*def teardown_function*") + + +def test_pytest_fail_pytrace_false_collection(testdir) -> None: """Test pytest.fail(..., pytrace=False) does not show tracebacks during collection.""" testdir.makepyfile( """ @@ -660,7 +676,7 @@ def some_internal_function(): result.stdout.no_fnmatch_line("*def some_internal_function()*") -def test_pytest_fail_notrace_non_ascii(testdir) -> None: +def test_pytest_fail_pytrace_false_non_ascii(testdir) -> None: """Fix pytest.fail with pytrace=False with non-ascii characters (#1178). This tests with native and unicode strings containing non-ascii chars.