Change default, add more tests, add changelog entry

This commit is contained in:
Brian Okken 2024-04-28 12:56:28 -07:00
parent 7320171f70
commit a9e8ead563
3 changed files with 52 additions and 11 deletions

View File

@ -0,0 +1,16 @@
Add `--xfail-tb` flag, which acts a lot like `--tb`, but controls the traceback output for XFAIL results.
* The `--xfail-tb` flag takes one argument, `auto|long|short|line|native|no`, and defaults to `no`.
* To turn on xfail tracebacks, pass in `-rx` or `-ra` along with `--xfail-tb=short` (or any of `auto`, `long`, `short`, `line`, or `native`).
The `--tb` flag, that controls normal test failure tracebacks, defaults to `auto`.
This change really only separates the traceback behavior of normal vs xfail failures.
Some history:
* This is a behavior break, but brings default behavior back to pre-8.0.0 behavior.
* With this change, default `-rx`/ `-ra` behavior is identical to pre-8.0 with respect to xfail tracebacks.
* With pytest 8.0, `-rx` or `-ra` would not only turn on summary reports for xfail, but also report the tracebacks for xfail results.
* This caused issues with some projects that utilize xfail, but don't want to see all of the xfail tracebacks.

View File

@ -221,7 +221,7 @@ def pytest_addoption(parser: Parser) -> None:
metavar="style",
action="store",
dest="xfail_tbstyle",
default="auto",
default="no",
choices=["auto", "long", "short", "no", "line", "native"],
help="Traceback print mode for xfail (auto/long/short/line/native/no)",
)
@ -1091,7 +1091,7 @@ class TerminalReporter:
which_reports: str,
sep_title: str,
needed_opt: Optional[str] = None,
style=None,
style: Optional[str] = None,
) -> None:
if style is None:
style = self.config.option.tbstyle

View File

@ -2909,7 +2909,7 @@ def test_summary_xfail_reason(pytester: Pytester) -> None:
assert result.stdout.lines.count(expect2) == 1
def test_summary_xfail_tb(pytester: Pytester) -> None:
def test_xfail_tb_default(pytester: Pytester) -> None:
pytester.makepyfile(
"""
import pytest
@ -2920,7 +2920,33 @@ def test_summary_xfail_tb(pytester: Pytester) -> None:
assert a == b
"""
)
# defaults to "no", so this is the same as `runpytest("-rx", "--xfail-tb=no")`
result = pytester.runpytest("-rx")
# Don't show traceback
result.stdout.no_fnmatch_line("*= XFAILURES =*")
result.stdout.no_fnmatch_line("*test_xfail_tb_line.py:6: assert 1 == 2")
# still print summary
result.stdout.fnmatch_lines(
[
"*= short test summary info =*",
"XFAIL test_xfail_tb_default.py::test_xfail",
"*= 1 xfailed in * =*",
]
)
def test_xfail_tb_short(pytester: Pytester) -> None:
pytester.makepyfile(
"""
import pytest
@pytest.mark.xfail
def test_xfail():
a, b = 1, 2
assert a == b
"""
)
result = pytester.runpytest("-rx", "--xfail-tb=short")
result.stdout.fnmatch_lines(
[
"*= XFAILURES =*",
@ -2928,11 +2954,10 @@ def test_summary_xfail_tb(pytester: Pytester) -> None:
"*@pytest.mark.xfail*",
"*def test_xfail():*",
"* a, b = 1, 2*",
"> *assert a == b*",
"E *assert 1 == 2*",
"test_summary_xfail_tb.py:6: AssertionError*",
"*> *assert a == b*",
"*E *assert 1 == 2*",
"*= short test summary info =*",
"XFAIL test_summary_xfail_tb.py::test_xfail",
"XFAIL test_xfail_tb_short.py::test_xfail",
"*= 1 xfailed in * =*",
]
)