Skip to content

Difference of behavior in assertRaises when used as context manager #601

@stephenfin

Description

@stephenfin

assertRaises can be used as a context manager since 2.8.0 (#443). However, the behavior is different (and IMO inferior) to the unittest behavior. For example, with unittest:

with self.assertRaises(BazException) as exc:
    foo('a', 'b', bar=123)

self.assertIn('baz thing', str(BazException))

This is not possible since exc is an instance of _AssertRaisesContext rather than the exception we've captured. To access the exception, I believe we need to use the .exception attribute.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions