Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --no-trace option, overrides pytrace=True for pytest.fail #6652

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Grig Gheorghiu
Grigorii Eremeev (budulianin)
Guido Wesdorp
Guoqiang Zhang
Guy Perry
Harald Armin Massa
Henk-Jaap Wagenaar
Holger Kohr
Expand Down
2 changes: 2 additions & 0 deletions changelog/6651.feature.rst
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 7 additions & 0 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
22 changes: 19 additions & 3 deletions testing/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand All @@ -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(
"""
Expand All @@ -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.
Expand Down