Skip to main content

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 "SomeError"

# Also good!
with assert_raises(contains="Some"):
raise "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 "OtherError"

Fields

  • message_contains (Optional[String]): If present, check that the error message contains this literal string.

Implemented traits

AnyType

Methods

__init__

__init__(inout self: Self, /)

Construct a context manager with no message pattern.

__init__(inout self: Self, /, *, contains: String)

Construct a context manager matching specific errors.

Args:

  • contains (String): The test will only pass if the error message includes the literal text passed.

__enter__

__enter__(self: Self)

Enter the context manager.

__exit__

__exit__(self: Self)

Exit the context manager with no error.

Raises:

AssertionError: Always. The block must raise to pass the test.

__exit__(self: Self, error: Error) -> Bool

Exit the context manager with an error.

Raises:

Error: If the error raised doesn't match the expected error to raise.