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.
  • call_location (_SourceLocation): Assigned the value returned by __call_locations() at Self.init.

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.