assert_raises | Modular
Mojo struct
struct assert_raises
Context manager that asserts that the block raises an exception.
You can use this to test expected error cases, and to test that the correct errors are raised. For instance:
from testing import assert_raises
# Good! Caught the raised error, test passes
with assert_raises():
raise Error("SomeError")
# Also good!
with assert_raises(contains="Some"):
raise Error("SomeError")
# This will assert, we didn't raise
with assert_raises():
pass
# This will let the underlying error propagate, failing the test
with assert_raises(contains="Some"):
raise Error("OtherError")Fields
- message_contains (
Optional[String]): If present, check that the error message contains this literal string. - call_location (
SourceLocation): Assigned the value returned by call_locations() at Self.init.
Implemented traits
AnyType,
ImplicitlyDestructible
comptime members
__del__is_trivial
comptime __del__is_trivial = _all_trivial_del[_NoneType, String]()
Methods
__init__
__init__(out self, *, location: Optional[SourceLocation] = None)
Construct a context manager with no message pattern.
Args:
- location (
Optional): The location of the error (defaults tocall_location).
__init__(out self, *, contains: String, location: Optional[SourceLocation] = None)
Construct a context manager matching specific errors.
Args:
__enter__
__enter__(self)
Enter the context manager.
__exit__
__exit__(self)
Exit the context manager with no error.
Raises:
AssertionError: Always. The block must raise to pass the test.