Exception & callable assertions¶
Assertions for callables: expected exceptions and the value returned by a call.
Expected exception mixin.
raises ¶
Asserts that val is callable and set the expected exception.
Just sets the expected exception, but never calls val, and therefore never fails. You must
chain to when_called_with() to invoke val().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ex
|
type
|
the expected exception |
required |
Examples:
Usage:
assert_that(some_func).raises(RuntimeError).when_called_with('foo')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns a new instance (with the expected exception) to chain the next assertion |
Source code in assertpy2/exception.py
when_called_with ¶
Asserts that val, when invoked with the given args and kwargs, meets the set expectation.
Invokes val() with the given args and kwargs. You must first set an expectation with
raises() or
does_not_raise() (expected exception),
or with
warns() or
does_not_warn() (expected warning).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*some_args
|
object
|
the args to call |
()
|
**some_kwargs
|
object
|
the kwargs to call |
{}
|
Examples:
Usage:
def some_func(a):
raise RuntimeError('some error!')
assert_that(some_func).raises(RuntimeError).when_called_with('foo')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns a new instance (now with the captured exception or warning message as the val) to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not meet the set expectation |
TypeError
|
if no expectation set first |
Source code in assertpy2/exception.py
returned ¶
Pivots the chain to the value val() returned during
when_called_with().
Use after a call that completed normally (warns(),
does_not_warn(), or
does_not_raise()) to assert
on the return value in the same chain.
Examples:
Usage:
assert_that(make_client).warns(DeprecationWarning).when_called_with().returned().is_instance_of(Client)
assert_that(adder).does_not_raise(TypeError).when_called_with(1, 2).returned().is_equal_to(3)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
a new instance wrapping the captured return value |
Raises:
| Type | Description |
|---|---|
TypeError
|
if no return value was captured (the call raised, or
|
Source code in assertpy2/exception.py
raised ¶
Pivots the chain to the exception object caught by
when_called_with(), to assert on its type,
args, or custom attributes - not only its message string.
Examples:
Usage:
err = assert_that(load).raises(ConfigError).when_called_with("bad").raised().value
assert_that(err.code).is_equal_to(42)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
a new instance wrapping the caught exception object |
Raises:
| Type | Description |
|---|---|
TypeError
|
if no exception was captured (the call did not raise, or
|
Source code in assertpy2/exception.py
caused_by ¶
Asserts the caught exception was chained from a cause of type ex (raise ... from, or an
exception raised during handling), then pivots the chain to that cause's message.
Examples:
Usage:
assert_that(save).raises(ServiceError).when_called_with(row).caused_by(TimeoutError)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ex
|
type
|
the expected cause type |
required |
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
a new instance wrapping the cause's message (chain on it, or walk deeper) |
Source code in assertpy2/exception.py
has_root_cause ¶
Asserts the root of the caught exception's cause chain is of type ex, then pivots the chain
to that root cause's message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ex
|
type
|
the expected root-cause type |
required |
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
a new instance wrapping the root cause's message |
Source code in assertpy2/exception.py
contains_error ¶
Asserts the caught exception is an exception group that contains, recursively, an exception of
each given type (for raises(ExceptionGroup)).
Examples:
Usage:
assert_that(run_tasks).raises(ExceptionGroup).when_called_with().contains_error(ValueError, KeyError)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*ex_types
|
type
|
the exception types the group must contain |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
this instance, to chain further assertions on the group |
Source code in assertpy2/exception.py
does_not_raise ¶
Asserts that val is callable and sets the not-expected exception.
Just sets the not-expected exception, but never calls val. You must
chain to when_called_with() to invoke val().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ex
|
type
|
the exception that should not be raised |
required |
Examples:
Usage:
assert_that(some_func).does_not_raise(RuntimeError).when_called_with('foo')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns a new instance to chain to the next assertion |