Skip to content

Exception should be direct cause of AssertionError from raises context manager on failure for exception to match pattern rather than context #14389

@jonathandung

Description

@jonathandung

What's the problem this feature will solve?

Currently, the AssertionError: Regex pattern did not match thrown on exit of a pytest.raises context when an exception was raised but the provided pattern was not matched is attached to the exception actually raised as its __context__ attribute implicitly. This causes the test failure report to incorrectly claim that 'During handling of the above exception, another exception occurred'.

Describe the solution you'd like

It should be attached to the __cause__ attribute instead, such that the connective would be 'The above exception was the direct cause of the following exception', which is more correct. Conceptually, that would mean doing something along the lines of the following:

raise AssertionError('Regex pattern did not match') from exc

where exc is the exception having occurred. However, I'm not sure where the logic is; I think it's in the matches method of the RaisesExc class.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions