Skip to content

Commit f6bdd71

Browse files
committed
Add --no-trace option, overrides pytrace=True for pytest.fail
1 parent a9c5d31 commit f6bdd71

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

changelog/6651.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add the --no-trace flag, allowing to override `pytrace=True` for `pytest.fail` calls.
2+
This is useful to avoid automated `pytest` runs outputting unwanted information.

src/_pytest/main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ def pytest_addoption(parser):
180180
default=False,
181181
help="Don't ignore tests in a local virtualenv directory",
182182
)
183+
group.addoption(
184+
"--notrace",
185+
"--no-trace",
186+
action="store_true",
187+
default=False,
188+
help="Do not output any traceback in case of failure, even when passing pytrace=True"
189+
)
183190

184191
group = parser.getgroup("debugconfig", "test session debugging and configuration")
185192
group.addoption(

src/_pytest/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def _repr_failure_py(
287287
self, excinfo: ExceptionInfo[Union[Failed, FixtureLookupError]], style=None
288288
) -> Union[str, ReprExceptionInfo, ExceptionChainRepr, FixtureLookupErrorRepr]:
289289
if isinstance(excinfo.value, Failed):
290-
if not excinfo.value.pytrace:
290+
if (self.config is not None and self.config.getoption("notrace")) or not excinfo.value.pytrace:
291291
return str(excinfo.value)
292292
if isinstance(excinfo.value, FixtureLookupError):
293293
return excinfo.value.formatrepr()

testing/test_runner.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ def pytest_sessionstart():
629629
assert result.ret == 98
630630

631631

632-
def test_pytest_fail_notrace_runtest(testdir) -> None:
632+
def test_pytest_fail_pytrace_false_runtest(testdir) -> None:
633633
"""Test pytest.fail(..., pytrace=False) does not show tracebacks during test run."""
634634
testdir.makepyfile(
635635
"""
@@ -645,7 +645,23 @@ def teardown_function(function):
645645
result.stdout.no_fnmatch_line("*def teardown_function*")
646646

647647

648-
def test_pytest_fail_notrace_collection(testdir) -> None:
648+
def test_pytest_fail_notrace_pytrace_true_runtest(testdir) -> None:
649+
"""Test pytest.fail(..., pytrace=True) does not show tracebacks when passing --no-trace."""
650+
testdir.makepyfile(
651+
"""
652+
import pytest
653+
def test_hello():
654+
pytest.fail("hello", pytrace=True)
655+
def teardown_function(function):
656+
pytest.fail("world", pytrace=True)
657+
"""
658+
)
659+
result = testdir.runpytest("--no-trace")
660+
result.stdout.fnmatch_lines(["world", "hello"])
661+
result.stdout.no_fnmatch_line("*def teardown_function*")
662+
663+
664+
def test_pytest_fail_pytrace_false_collection(testdir) -> None:
649665
"""Test pytest.fail(..., pytrace=False) does not show tracebacks during collection."""
650666
testdir.makepyfile(
651667
"""
@@ -660,7 +676,7 @@ def some_internal_function():
660676
result.stdout.no_fnmatch_line("*def some_internal_function()*")
661677

662678

663-
def test_pytest_fail_notrace_non_ascii(testdir) -> None:
679+
def test_pytest_fail_pytrace_false_non_ascii(testdir) -> None:
664680
"""Fix pytest.fail with pytrace=False with non-ascii characters (#1178).
665681
666682
This tests with native and unicode strings containing non-ascii chars.

0 commit comments

Comments
 (0)