Skip to content

Generic

The generic checks work for every value. assert_that returns an ObjectAssert for values without a type-specific assertion class, for example None or instances of your own classes, and every type-specific class inherits these checks.

Each check has its negation:

Check Passes if
is_equal_to, is_not_equal_to the value is (not) equal, with ==
is_same_as, is_not_same_as the value is (not) the same object, with is
is_none, is_not_none the value is (not) None
is_instance_of, is_not_instance_of the value is (not) an instance of a type
assert_that(user, description="current user").is_not_none().is_instance_of(User)
assert_that(response.json()).is_equal_to({"id": 1, "name": "Alice"})

Equal or the same?

is_equal_to compares the content, is_same_as the identity. Two lists with the same items are equal but separate objects:

items = [1, 2]
copy = list(items)

assert_that(copy).is_equal_to(items)  # passes
assert_that(copy).is_same_as(items)  # fails
assert_that(copy).is_not_same_as(items)  # passes

Use is_same_as only when the identity matters, for example for a cached or shared instance. For numbers and strings, use is_equal_to: whether two equal numbers are the same object depends on the Python interpreter.

None and falsy values

is_none checks for None only. Falsy values such as 0, "" or [] are not None:

assert_that(0).is_not_none()  # passes

Types and subclasses

is_instance_of uses isinstance, so instances of subclasses pass as well. A bool is an instance of int, and a datetime is an instance of date:

assert_that(True).is_instance_of(int)  # passes

Generic assertions that work for every value, and the base of all assertion classes.

assert_that returns this class for values without a type-specific assertion class, for example None or instances of your own classes. All type-specific classes, such as NumberAssert or StringAssert, inherit its assertions.

The generic checks, each with its negation:

  • Equality: is_equal_to, is_not_equal_to
  • Identity: is_same_as, is_not_same_as
  • None: is_none, is_not_none
  • Type: is_instance_of, is_not_instance_of

Every check returns the assertion itself, so checks can be chained. A failed check raises AssertionFailedError.

Examples:

assert_that(user, description="current user").is_not_none().is_instance_of(User)
assert_that(cache.get("config")).is_same_as(config)

Parameters:

Name Type Description Default
actual T

The value to check.

required
description str | None

A name for the value, shown in failure messages, e.g. "status code".

None

is_equal_to(expected)

Check that the value is equal to the expected value, using ==.

Examples:

assert_that(response.status_code).is_equal_to(200)
assert_that([1, 2]).is_equal_to([1, 2])

Parameters:

Name Type Description Default
expected object

The value that the checked value must be equal to.

required

Returns:

Type Description
Self

This assertion, so that further checks can be chained.

Raises:

Type Description
AssertionFailedError

If the value is not equal to the expected value.

is_instance_of(expected_type)

Check that the value is an instance of the expected type or of one of its subclasses.

Examples:

assert_that(response.json()).is_instance_of(dict)
assert_that(True).is_instance_of(int)

Parameters:

Name Type Description Default
expected_type type[object]

The type that the checked value must be an instance of.

required

Returns:

Type Description
Self

This assertion, so that further checks can be chained.

Raises:

Type Description
AssertionFailedError

If the value is not an instance of the expected type.

is_none()

Check that the value is None.

Examples:

assert_that(user.deleted_at).is_none()

Returns:

Type Description
Self

This assertion, so that further checks can be chained.

Raises:

Type Description
AssertionFailedError

If the value is not None.

is_not_equal_to(expected)

Check that the value is not equal to the given value, using !=.

Examples:

assert_that(new_password).is_not_equal_to(old_password)

Parameters:

Name Type Description Default
expected object

The value that the checked value must not be equal to.

required

Returns:

Type Description
Self

This assertion, so that further checks can be chained.

Raises:

Type Description
AssertionFailedError

If the value is equal to the given value.

is_not_instance_of(expected_type)

Check that the value is not an instance of the given type or of one of its subclasses.

Examples:

assert_that(result).is_not_instance_of(Exception)

Parameters:

Name Type Description Default
expected_type type[object]

The type that the checked value must not be an instance of.

required

Returns:

Type Description
Self

This assertion, so that further checks can be chained.

Raises:

Type Description
AssertionFailedError

If the value is an instance of the given type.

is_not_none()

Check that the value is not None.

Examples:

assert_that(user.id).is_not_none()

Returns:

Type Description
Self

This assertion, so that further checks can be chained.

Raises:

Type Description
AssertionFailedError

If the value is None.

is_not_same_as(expected)

Check that the value is a different object than the given one, using is not.

Examples:

copy = original.copy()
assert_that(copy).is_not_same_as(original)

Parameters:

Name Type Description Default
expected object

The object that the checked value must not be.

required

Returns:

Type Description
Self

This assertion, so that further checks can be chained.

Raises:

Type Description
AssertionFailedError

If the value is the same object.

is_same_as(expected)

Check that the value is the same object as the expected one, using is.

Examples:

assert_that(get_config()).is_same_as(get_config())

Parameters:

Name Type Description Default
expected object

The object that the checked value must be.

required

Returns:

Type Description
Self

This assertion, so that further checks can be chained.

Raises:

Type Description
AssertionFailedError

If the value is a different object.