Core & object assertions¶
Assertions available on every value: equality, identity, type, None, truthiness, satisfies,
structural matching, and recursive field checks. A type checker offers matches_structure() on a
mapping, a model or an object, and not on a number, a string, a collection, bytes, a path or a date.
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 assertpy/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 that widens |
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 assertpy/base.py
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 | |
is_not_equal_to ¶
Asserts that val is not equal to other.
Checks actual is not equal to expected using the == operator, the question is_equal_to
asks. != is never asked: a type's __ne__ need not be the negation of its __eq__.
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 assertpy/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 assertpy/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 assertpy/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 |
TypeError
|
if val is a coroutine, since every coroutine is truthy and this would hold whatever awaiting it would have answered |
Source code in assertpy/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 |
TypeError
|
if val is a coroutine, since every coroutine is truthy and this would refuse whatever awaiting it would have answered |
Source code in assertpy/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 assertpy/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 assertpy/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 assertpy/base.py
is_instance_of ¶
Asserts that val is an instance of the given class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
some_class
|
ClassInfo
|
the expected class, a union of them, or a tuple nested to any depth |
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)
With alternatives, anything isinstance takes, nested to any depth:
assert_that(1).is_instance_of(int | str)
assert_that(1).is_instance_of((int, str))
assert_that(1).is_instance_of((int, (str, bytes)))
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 |
TypeError
|
if the given arg, or any member of a union or tuple, is not a class |
Source code in assertpy/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
|
ClassInfo
|
the candidate classes, each a class, a union of them, or a nested tuple |
()
|
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)
assert_that(1).is_instance_of_any(int | str, (bytes, bytearray))
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 assertpy/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
|
ClassInfo
|
the expected ancestor class, a union of them, or a tuple nested to any depth |
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, or any member of a union or tuple, is not a class |
Source code in assertpy/base.py
is_length ¶
Asserts that val is the given length.
Checks val is the given length using the len() built-in. A matcher in place of the number is
asked about the length, so a bound or a range reads as one assertion: is_length(match.between(2,
5)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int | Matcher
|
the expected length, or a matcher the length has to satisfy |
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)
assert_that(['a', 'b']).is_length(match.greater_than(1))
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 assertpy/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
|
SupportsIndex
|
the inclusive lower length bound |
required |
high
|
SupportsIndex
|
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 assertpy/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. A Pydantic model is
walked through the values its fields hold, not its model_dump(): a field excluded from the
dump is walked, a computed field is not, and a field serializer does not apply.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matcher
|
Matcher[Any] | Callable[..., bool]
|
a |
required |
Examples:
Usage:
from assertpy 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 assertpy/_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, reading a Pydantic model as that does.
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 assertpy/_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 assertpy 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 assertpy/_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 assertpy 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 assertpy/_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.
A failure prints the spec with what matched left out as .., and val the same way where it is read
as plain dicts with plain keys, a model and an attrs instance among them. It holds both whole.
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 assertpy 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 assertpy/_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 assertpy/_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 assertpy/_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 assertpy 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 assertpy/_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 assertpy 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 assertpy/_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 assertpy 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 assertpy/_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 assertpy 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 assertpy/_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 assertpy 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 assertpy/_satisfies.py
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 | |
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 |