Skip to content

ExceptionInfo.for_later() wont add assertion strip text #12175

Open
@picnixz

Description

@picnixz

Using exc_info.exconly(tryshort=True) is meant to remove AssertionError: from the exception message according to (ref)

When 'tryshort' resolves to True, and the exception is an AssertionError, only the actual exception part of the exception representation is returned (so 'AssertionError: ' is removed from the beginning).

However, this is not the case. In addition, the tests do not assert this behaviour:

def test_excinfo_exconly():
excinfo = pytest.raises(ValueError, h)
assert excinfo.exconly().startswith("ValueError")
with pytest.raises(ValueError) as excinfo:
raise ValueError("hello\nworld")
msg = excinfo.exconly(tryshort=True)
assert msg.startswith("ValueError")
assert msg.endswith("world")

MWE

cat <<-EOF > excinfo_exconly.py
import pytest

def test():
    with pytest.raises(AssertionError) as exc_info:
        raise AssertionError('message')

    assert exc_info.exconly(tryshort=True) == 'message'
EOF
pytest excinfo_exconly.py

Environment

Platform:              linux; (Linux-5.14.21-150500.55.52-default-x86_64-with-glibc2.31)
Python version:        3.10.13 (main, Sep 05 2023, 11:46:10) [GCC])
Python implementation: CPython
Pytest version:		   8.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions