Core & object assertions¶
Assertions available on every value: equality, identity, type, None, truthiness, satisfies,
structural matching, and recursive field checks.
Base mixin.
described_as ¶
Describes the assertion. On failure, the description is included in the error message.
This is not an assertion itself. But if the any of the following chained assertions fail, the description will be included in addition to the regular error message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
description
|
str
|
the error message description |
required |
Examples:
Usage:
assert_that(1).described_as('error msg desc').is_equal_to(2) # fails
# [error msg desc] Expected <1> to be equal to <2>, but was not.
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Source code in assertpy2/base.py
is_equal_to ¶
Asserts that val is equal to other.
Checks actual is equal to expected using the == operator. When val is dict-like
or has introspectable fields (dataclass, namedtuple, attrs, Pydantic model),
optionally ignore or include keys/fields when checking equality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
the expected value |
required |
**kwargs
|
object
|
see below |
{}
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
ignore |
Hashable | list | set | frozenset | None
|
the key/field (or list/set/frozenset of
keys/fields) to ignore. Besides exact keys and nested-path tuples, a |
include |
Hashable | list | set | frozenset | None
|
the key/field (or list/set/frozenset of
keys/fields) to include. Accepts the same |
tolerance |
float | None
|
an absolute tolerance applied to every real-number leaf anywhere in
the structure, so close floats compare equal ( |
comparators |
dict | None
|
a dict mapping a |
Examples:
Usage:
assert_that(1 + 2).is_equal_to(3)
assert_that('foo').is_equal_to('foo')
assert_that(123).is_equal_to(123)
assert_that(123.4).is_equal_to(123.4)
assert_that(['a', 'b']).is_equal_to(['a', 'b'])
assert_that((1, 2, 3)).is_equal_to((1, 2, 3))
assert_that({'a': 1, 'b': 2}).is_equal_to({'a': 1, 'b': 2})
assert_that({'a', 'b'}).is_equal_to({'a', 'b'})
When the val is dict-like, keys can optionally be ignored when checking equality:
# ignore a single key
assert_that({'a': 1, 'b': 2}).is_equal_to({'a': 1}, ignore='b')
# ignore multiple keys
assert_that({'a': 1, 'b': 2, 'c': 3}).is_equal_to({'a': 1}, ignore=['b', 'c'])
# ignore nested keys
assert_that({'a': {'b': 2, 'c': 3, 'd': 4}}).is_equal_to(
{'a': {'d': 4}}, ignore=[('a', 'b'), ('a', 'c')]
)
When the val is dict-like, only certain keys can be included when checking equality:
# include a single key
assert_that({'a': 1, 'b': 2}).is_equal_to({'a': 1}, include='a')
# include multiple keys
assert_that({'a': 1, 'b': 2, 'c': 3}).is_equal_to({'a': 1, 'b': 2}, include=['a', 'b'])
Works with dataclasses, namedtuples, attrs, and Pydantic models:
@dataclass
class User:
id: int
name: str
assert_that(User(id=1, name="Alice")).is_equal_to(User(id=99, name="Alice"), ignore="id")
Compares lists of objects pairwise:
actual = [User(id=1, name="Alice"), User(id=2, name="Bob")]
expected = [User(id=99, name="Alice"), User(id=99, name="Bob")]
assert_that(actual).is_equal_to(expected, ignore="id")
Compare nested floats with an absolute tolerance, or supply custom comparators:
assert_that({"price": 1.0001}).is_equal_to({"price": 1.0}, tolerance=0.001)
# by type, or by field name (field name wins over type)
assert_that(actual).is_equal_to(expected, comparators={float: lambda a, e: round(a, 2) == round(e, 2)})
assert_that(actual).is_equal_to(expected, comparators={"name": lambda a, e: a.lower() == e.lower()})
Ignore fields by regex or by type:
import re
assert_that(payload).is_equal_to(expected, ignore=re.compile(r"^_")) # ignore private-ish keys
assert_that(payload).is_equal_to(expected, ignore=float) # ignore all float fields
Failure produces a nice error message:
assert_that(1).is_equal_to(2) # fails
# Expected <1> to be equal to <2>, but was not.
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if actual is not equal to expected |
TypeError
|
if |
ValueError
|
if |
Tip
Using is_equal_to() with a float val is just
asking for trouble. Instead, you'll
always want to use fuzzy numeric assertions
like is_close_to()
or is_between().
See Also
is_equal_to_ignoring_case() -
for case-insensitive string equality
Source code in assertpy2/base.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | |
all_fields_satisfy ¶
Asserts that every scalar leaf in val's object graph satisfies the given matcher.
Walks val recursively (mappings, dataclasses, namedtuples, Pydantic models, lists, tuples) and applies the matcher to each leaf value, reporting the path of every leaf that does not satisfy it. Scalars, strings, sets and opaque objects are treated as single leaves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matcher
|
Matcher | Callable[..., bool]
|
a |
required |
Examples:
Usage:
from assertpy2 import match
assert_that({"a": 1, "nested": {"b": 2}}).all_fields_satisfy(match.is_positive())
assert_that([1, [2, 3]]).all_fields_satisfy(lambda x: x > 0)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if any leaf does not satisfy the matcher |
TypeError
|
if matcher is neither a Matcher nor callable |
Source code in assertpy2/base.py
has_no_none_fields ¶
Asserts that no scalar leaf in val's object graph is None.
Convenience wrapper over all_fields_satisfy() with a not-None matcher; reports the path
of every None leaf found anywhere in the graph.
Examples:
Usage:
assert_that({"id": 1, "profile": {"name": "Alice"}}).has_no_none_fields()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if any leaf is |
Source code in assertpy2/base.py
satisfies ¶
Asserts that val satisfies the given matcher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matcher
|
Matcher | Callable[..., bool]
|
a |
required |
Examples:
Usage with matchers:
from assertpy2 import match
assert_that(7).satisfies(match.greater_than(5) & match.less_than(10))
assert_that('hello').satisfies(match.starts_with('he'))
Usage with callables:
assert_that(42).satisfies(lambda x: x % 2 == 0)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not satisfy the matcher |
Source code in assertpy2/base.py
each ¶
Asserts that every item in val satisfies the given matcher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matcher
|
Matcher | Callable[..., bool]
|
a |
required |
Examples:
Usage with matchers:
from assertpy2 import match
assert_that([1, 2, 3]).each(match.is_positive())
assert_that([10, 20, 30]).each(match.between(1, 100))
Usage with extracting:
assert_that(users).extracting('age').each(match.between(18, 120))
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if any item does not satisfy the matcher |
Source code in assertpy2/base.py
matches_structure ¶
Asserts that val matches the given structure specification.
val may be a dict or a pydantic-style model (anything exposing model_dump()), which is
normalized to its dict before matching. Each key in spec maps to either a
Matcher, a raw value (checked via ==), or a nested dict
for recursive matching. Extra keys in val that are absent from the spec are allowed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
dict[Any, Any]
|
a dict where values can be Matcher instances, raw values, or nested dicts |
required |
Examples:
Usage:
from assertpy2 import assert_that, match
user = {"name": "Alice", "age": 30, "id": "550e8400-e29b-41d4-a716-446655440000"}
assert_that(user).matches_structure({
"name": match.is_non_empty_string(),
"age": match.between(18, 120),
"id": match.is_uuid(),
})
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not match the structure spec |
Source code in assertpy2/base.py
is_callable ¶
Asserts that val is callable.
Examples:
Usage:
assert_that(lambda: None).is_callable()
assert_that(print).is_callable()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not callable |
Source code in assertpy2/base.py
is_not_callable ¶
Asserts that val is not callable.
Examples:
Usage:
assert_that(42).is_not_callable()
assert_that('foo').is_not_callable()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is callable |
Source code in assertpy2/base.py
any_satisfy ¶
Asserts that at least one item in val satisfies the given matcher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matcher
|
Matcher | Callable[..., bool]
|
a |
required |
Examples:
Usage with matchers:
from assertpy2 import match
assert_that([1, -2, 3]).any_satisfy(match.is_negative())
Usage with callables:
assert_that([1, 2, 3]).any_satisfy(lambda x: x > 2)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if no item satisfies the matcher |
Source code in assertpy2/base.py
all_satisfy ¶
Asserts that all items in val satisfy the given matcher.
Semantic alias for each().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matcher
|
Matcher | Callable[..., bool]
|
a |
required |
Examples:
Usage with matchers:
from assertpy2 import match
assert_that([1, 2, 3]).all_satisfy(match.is_positive())
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if any item does not satisfy the matcher |
Source code in assertpy2/base.py
none_satisfy ¶
Asserts that no item in val satisfies the given matcher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matcher
|
Matcher | Callable[..., bool]
|
a |
required |
Examples:
Usage with matchers:
from assertpy2 import match
assert_that([1, 2, 3]).none_satisfy(match.is_negative())
Usage with callables:
assert_that([1, 2, 3]).none_satisfy(lambda x: x < 0)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if any item satisfies the matcher |
Source code in assertpy2/base.py
satisfies_exactly ¶
Asserts that val has exactly one item per matcher, each satisfying the matcher at its position.
Unlike each() (one matcher applied to every item), this
pairs the i-th item with the
i-th matcher and additionally requires the lengths to match. Every positional mismatch is
reported, not just the first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*matchers
|
Matcher | Callable[..., bool]
|
one |
()
|
Examples:
Usage:
from assertpy2 import match
assert_that([1, "foo", 3.0]).satisfies_exactly(
match.is_odd(), match.is_instance_of(str), match.is_positive()
)
assert_that([2, 4]).satisfies_exactly(lambda x: x == 2, lambda x: x == 4)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if the length differs, or any item does not satisfy its matcher |
TypeError
|
if val is not iterable, or a given arg is neither a Matcher nor callable |
ValueError
|
if no matchers are given |
Source code in assertpy2/base.py
zip_satisfies ¶
Asserts that each pair from zipping val with other satisfies the two-arg predicate.
Pairs the i-th item of val with the i-th item of other and checks predicate(a, b).
The two iterables must have equal length. Every failing pair is reported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Iterable[object]
|
the iterable to zip with val |
required |
predicate
|
Callable[..., bool]
|
a two-arg callable returning a bool, applied to each |
required |
Examples:
Usage:
assert_that([1, 2, 3]).zip_satisfies([2, 4, 6], lambda a, b: b == a * 2)
assert_that(["a", "bb"]).zip_satisfies([1, 2], lambda s, n: len(s) == n)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if the lengths differ, or any pair does not satisfy the predicate |
TypeError
|
if val or other is not iterable, or predicate is not callable |
Source code in assertpy2/base.py
is_not_equal_to ¶
Asserts that val is not equal to other.
Checks actual is not equal to expected using the != operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
the expected value |
required |
Examples:
Usage:
assert_that(1 + 2).is_not_equal_to(4)
assert_that('foo').is_not_equal_to('bar')
assert_that(123).is_not_equal_to(456)
assert_that(123.4).is_not_equal_to(567.8)
assert_that(['a', 'b']).is_not_equal_to(['c', 'd'])
assert_that((1, 2, 3)).is_not_equal_to((1, 2, 4))
assert_that({'a': 1, 'b': 2}).is_not_equal_to({'a': 1, 'b': 3})
assert_that({'a', 'b'}).is_not_equal_to({'a', 'x'})
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if actual is equal to expected |
TypeError
|
if val or other is an element-wise array/frame-like (numpy/pandas/polars) whose
|
Source code in assertpy2/base.py
is_same_as ¶
Asserts that val is identical to other.
Checks actual is identical to expected using the is operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
the expected value |
required |
Examples:
Basic types are identical:
assert_that(1).is_same_as(1)
assert_that('foo').is_same_as('foo')
assert_that(123.4).is_same_as(123.4)
As are immutables like tuple:
assert_that((1, 2, 3)).is_same_as((1, 2, 3))
But mutable collections like list, dict, and set are not:
# these all fail...
assert_that(['a', 'b']).is_same_as(['a', 'b']) # fails
assert_that({'a': 1, 'b': 2}).is_same_as({'a': 1, 'b': 2}) # fails
assert_that({'a', 'b'}).is_same_as({'a', 'b'}) # fails
Unless they are the same object:
x = {'a': 1, 'b': 2}
y = x
assert_that(x).is_same_as(y)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if actual is not identical to expected |
Source code in assertpy2/base.py
is_not_same_as ¶
Asserts that val is not identical to other.
Checks actual is not identical to expected using the is operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
the expected value |
required |
Examples:
Usage:
assert_that(1).is_not_same_as(2)
assert_that('foo').is_not_same_as('bar')
assert_that(123.4).is_not_same_as(567.8)
assert_that((1, 2, 3)).is_not_same_as((1, 2, 4))
# mutable collections, even if equal, are not identical...
assert_that(['a', 'b']).is_not_same_as(['a', 'b'])
assert_that({'a': 1, 'b': 2}).is_not_same_as({'a': 1, 'b': 2})
assert_that({'a', 'b'}).is_not_same_as({'a', 'b'})
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if actual is identical to expected |
Source code in assertpy2/base.py
is_true ¶
Asserts that val is true.
Examples:
Usage:
assert_that(True).is_true()
assert_that(1).is_true()
assert_that('foo').is_true()
assert_that(1.0).is_true()
assert_that(['a', 'b']).is_true()
assert_that((1, 2, 3)).is_true()
assert_that({'a': 1, 'b': 2}).is_true()
assert_that({'a', 'b'}).is_true()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is false |
Source code in assertpy2/base.py
is_false ¶
Asserts that val is false.
Examples:
Usage:
assert_that(False).is_false()
assert_that(0).is_false()
assert_that('').is_false()
assert_that(0.0).is_false()
assert_that([]).is_false()
assert_that(()).is_false()
assert_that({}).is_false()
assert_that(set()).is_false()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is true |
Source code in assertpy2/base.py
is_none ¶
Asserts that val is none.
Examples:
Usage:
assert_that(None).is_none()
assert_that(print('hello world')).is_none()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not none |
Source code in assertpy2/base.py
is_not_none ¶
Asserts that val is not none.
Examples:
Usage:
assert_that(0).is_not_none()
assert_that('foo').is_not_none()
assert_that(False).is_not_none()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is none |
Source code in assertpy2/base.py
is_type_of ¶
Asserts that val is of the given type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
some_type
|
type
|
the expected type |
required |
Examples:
Usage:
assert_that(1).is_type_of(int)
assert_that('foo').is_type_of(str)
assert_that(123.4).is_type_of(float)
assert_that(['a', 'b']).is_type_of(list)
assert_that((1, 2, 3)).is_type_of(tuple)
assert_that({'a': 1, 'b': 2}).is_type_of(dict)
assert_that({'a', 'b'}).is_type_of(set)
assert_that(True).is_type_of(bool)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not of the given type |
Source code in assertpy2/base.py
is_instance_of ¶
Asserts that val is an instance of the given class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
some_class
|
type
|
the expected class |
required |
Examples:
Usage:
assert_that(1).is_instance_of(int)
assert_that('foo').is_instance_of(str)
assert_that(123.4).is_instance_of(float)
assert_that(['a', 'b']).is_instance_of(list)
assert_that((1, 2, 3)).is_instance_of(tuple)
assert_that({'a': 1, 'b': 2}).is_instance_of(dict)
assert_that({'a', 'b'}).is_instance_of(set)
assert_that(True).is_instance_of(bool)
With a user-defined class:
class Foo: pass
f = Foo()
assert_that(f).is_instance_of(Foo)
assert_that(f).is_instance_of(object)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not an instance of the given class |
Source code in assertpy2/base.py
is_length ¶
Asserts that val is the given length.
Checks val is the given length using the len() built-in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int
|
the expected length |
required |
Examples:
Usage:
assert_that('foo').is_length(3)
assert_that(['a', 'b']).is_length(2)
assert_that((1, 2, 3)).is_length(3)
assert_that({'a': 1, 'b': 2}).is_length(2)
assert_that({'a', 'b'}).is_length(2)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not the given length |