class CustomException(ValueError):
def __init__(self, foo, bar):
self.foo = foo
self.bar = bar
I have a class with an exception like the above. foo
and bar
are used by the function raising the error to supply some extra information about the exception to the calling class.
I'm trying to test this behavior like this:
with pytest.raises(CustomException) as ce:
func_that_raises(broken_args)
assert ce.foo == 'blah'
assert not ce.bar # if `bar` is expected to be a boolean
However, ce
is an instance of ExceptionInfo
that py.test gives me, and not an instance of CustomException
. Is there any way to retain and inspect the original exception that was raised?
The actual exception object is available as ce.value
, so your asserts need to be written as:
assert ce.value.foo == 'blah'
assert not ce.value.bar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With