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

display single contained exception in excgroups in test summary #12975

Merged
merged 7 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
display single contained exception in excgroups in short test summary…
… info
  • Loading branch information
jakkdl committed Nov 18, 2024
commit 47996bdc3870f9eed82951a15e07f430eb27a665
1 change: 1 addition & 0 deletions changelog/12943.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
If a test fails with an exceptiongroup with a single exception, the contained exception will now be displayed in the short test summary info.
25 changes: 24 additions & 1 deletion src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,24 @@ def _truncate_recursive_traceback(

def repr_excinfo(self, excinfo: ExceptionInfo[BaseException]) -> ExceptionChainRepr:
repr_chain: list[tuple[ReprTraceback, ReprFileLocation | None, str | None]] = []

def _get_single_subexc(
eg: BaseExceptionGroup[BaseException],
) -> BaseException | None:
res: BaseException | None = None
for subexc in eg.exceptions:
if res is not None:
return None

if isinstance(subexc, BaseExceptionGroup):
res = _get_single_subexc(subexc)
if res is None:
# there were multiple exceptions in the subgroup
return None
else:
res = subexc
return res

e: BaseException | None = excinfo.value
excinfo_: ExceptionInfo[BaseException] | None = excinfo
descr = None
Expand All @@ -1041,6 +1059,7 @@ def repr_excinfo(self, excinfo: ExceptionInfo[BaseException]) -> ExceptionChainR
seen.add(id(e))

if excinfo_:
reprcrash = excinfo_._getreprcrash()
# Fall back to native traceback as a temporary workaround until
# full support for exception groups added to ExceptionInfo.
# See https://github.com/pytest-dev/pytest/issues/9159
Expand All @@ -1054,9 +1073,13 @@ def repr_excinfo(self, excinfo: ExceptionInfo[BaseException]) -> ExceptionChainR
)
)
)
if (
reprcrash is not None
and (subexc := _get_single_subexc(e)) is not None
):
reprcrash.message = f"[in {type(e).__name__}] {subexc!r}"
else:
reprtraceback = self.repr_traceback(excinfo_)
reprcrash = excinfo_._getreprcrash()
else:
# Fallback to native repr if the exception doesn't have a traceback:
# ExceptionInfo objects require a full traceback to work.
Expand Down
57 changes: 57 additions & 0 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,63 @@ def test_exceptiongroup(pytester: Pytester, outer_chain, inner_chain) -> None:
_exceptiongroup_common(pytester, outer_chain, inner_chain, native=False)


def test_exceptiongroup_short_summary_info(pytester: Pytester):
pytester.makepyfile(
"""
import sys

if sys.version_info < (3, 11):
from exceptiongroup import BaseExceptionGroup, ExceptionGroup

def test_base() -> None:
raise BaseExceptionGroup("NOT IN SUMMARY", [SystemExit("a" * 10)])

def test_nonbase() -> None:
raise ExceptionGroup("NOT IN SUMMARY", [ValueError("a" * 10)])

def test_nested() -> None:
raise ExceptionGroup(
"NOT DISPLAYED", [
ExceptionGroup("NOT IN SUMMARY", [ValueError("a" * 10)])
]
)

def test_multiple() -> None:
raise ExceptionGroup(
"b" * 10,
[
ValueError("NOT IN SUMMARY"),
TypeError("NOT IN SUMMARY"),
]
)
"""
)
result = pytester.runpytest("-vv")
assert result.ret == 1
result.stdout.fnmatch_lines(
[
"*= short test summary info =*",
(
"FAILED test_exceptiongroup_short_summary_info.py::test_base - "
"[in BaseExceptionGroup] SystemExit('aaaaaaaaaa')"
),
(
"FAILED test_exceptiongroup_short_summary_info.py::test_nonbase - "
"[in ExceptionGroup] ValueError('aaaaaaaaaa')"
),
(
"FAILED test_exceptiongroup_short_summary_info.py::test_nested - "
"[in ExceptionGroup] ValueError('aaaaaaaaaa')"
),
(
"FAILED test_exceptiongroup_short_summary_info.py::test_multiple - "
"ExceptionGroup: bbbbbbbbbb (2 sub-exceptions)"
),
"*= 4 failed in *",
]
)


@pytest.mark.parametrize("tbstyle", ("long", "short", "auto", "line", "native"))
def test_all_entries_hidden(pytester: Pytester, tbstyle: str) -> None:
"""Regression test for #10903."""
Expand Down
Loading