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 |
ignore_null |
bool
|
when |
strict_types |
bool
|
when |
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
Require the same type at every level, which plain == does not:
assert_that({"active": True}).is_equal_to({"active": 1}) # passes
assert_that({"active": True}).is_equal_to({"active": 1}, strict_types=True) # fails
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
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
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 (or contains, at any nesting depth) 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_instance_of_any ¶
Asserts that val is an instance of at least one of the given classes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*some_classes
|
type
|
the candidate classes |
()
|
Examples:
Usage:
assert_that(1).is_instance_of_any(int, float)
assert_that('foo').is_instance_of_any(str, bytes)
assert_that(TimeoutError()).is_instance_of_any(OSError, ValueError)
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 any of the given classes |
TypeError
|
if a given arg is not a class |
ValueError
|
if no classes are given |
Source code in assertpy2/base.py
is_subclass_of ¶
Asserts that val is a class and is a subclass of the given class.
Checks the class hierarchy using the issubclass() built-in, so a class counts as a
subclass of itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
some_class
|
type
|
the expected ancestor class |
required |
Examples:
Usage:
assert_that(bool).is_subclass_of(int)
assert_that(TimeoutError).is_subclass_of(OSError)
class Base: pass
class Derived(Base): pass
assert_that(Derived).is_subclass_of(Base)
assert_that(Derived).is_subclass_of(Derived)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not a subclass of the given class |
TypeError
|
if val or the given arg is not a 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 |
Source code in assertpy2/base.py
is_length_between ¶
Asserts that val's length is between low and high (both inclusive).
Checks val's length using the len() built-in, like
is_length(). Identical to
has_size_between() apart from
the error message wording.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
low
|
int
|
the inclusive lower length bound |
required |
high
|
int
|
the inclusive upper length bound |
required |
Examples:
Usage:
assert_that('foo').is_length_between(1, 5)
assert_that(['a', 'b']).is_length_between(2, 2)
assert_that((1, 2, 3)).is_length_between(0, 3)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val's length is not between low and high |
TypeError
|
if a given arg is not an int |
ValueError
|
if a given arg is negative, or low is greater than high |
Source code in assertpy2/base.py
Predicate and matcher-application assertions: satisfies / each / *_satisfy / matches_structure.
all_fields_satisfy ¶
all_fields_satisfy(
matcher: Matcher[Any] | Callable[..., bool],
*,
allow_empty: bool = False,
) -> Self
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[Any] | 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/_satisfies.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/_satisfies.py
satisfies ¶
Asserts that val satisfies the given matcher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matcher
|
Matcher[Any] | 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)
When the callable is typed with TypeIs it also narrows the chain to the guarded type
(refinement narrowing), so .value hands the value back typed - see
Type Safety for
checker support (advanced; not yet in PyCharm).
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/_satisfies.py
each ¶
Asserts that every item in val satisfies the given matcher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matcher
|
Matcher[Any] | 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/_satisfies.py
matches_structure ¶
Asserts that val matches the given structure specification.
val may be any mapping (a dict, a MappingProxyType, 3.15's frozendict, or your
own collections.abc.Mapping), a pydantic-style model (anything exposing model_dump()),
or an attrs instance, 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/_satisfies.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/_satisfies.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/_satisfies.py
any_satisfy ¶
Asserts that at least one item in val satisfies the given matcher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matcher
|
Matcher[Any] | 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/_satisfies.py
all_satisfy ¶
Asserts that all items in val satisfy the given matcher.
Semantic alias for each().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matcher
|
Matcher[Any] | 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/_satisfies.py
none_satisfy ¶
Asserts that no item in val satisfies the given matcher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matcher
|
Matcher[Any] | 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/_satisfies.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[Any] | 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/_satisfies.py
satisfies_exactly_in_any_order ¶
Asserts that the items and the given matchers can be paired one-to-one, in any order.
Like satisfies_exactly() but ignoring
positions: passes when some one-to-one assignment pairs every item with a distinct matcher
it satisfies. The lengths must still match, and no matcher may be reused for two items.
Since every matcher is probed against every item, a probe that raises TypeError (a
type-incompatible comparison on a mixed collection, like is_positive() meeting a string)
counts as a non-match instead of crashing the pairing. On failure, an unpaired matcher whose
probes raised is annotated with the raise count in the diff, so a buggy predicate is not
mistaken for an ordinary mismatch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*matchers
|
Matcher[Any] | Callable[..., bool]
|
one |
()
|
Examples:
Usage:
from assertpy2 import match
assert_that(["foo", 3]).satisfies_exactly_in_any_order(
match.is_odd(), match.is_instance_of(str)
)
assert_that([2, 1]).satisfies_exactly_in_any_order(lambda x: x == 1, lambda x: x == 2)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if the length differs, or no one-to-one pairing satisfies all matchers |
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/_satisfies.py
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 | |
zip_satisfies ¶
zip_satisfies(
other: Iterable[object],
predicate: Callable[..., bool],
*,
allow_empty: bool = False,
) -> Self
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 |