assertpy: Fluent, fully typed assertion library for Python: your type checker sees only the assertions that fit the value, and every failure carries a structured diff. Soft assertions, polling, inline snapshots, OpenAPI contracts. · Pythie
Fluent, fully typed assertion library for Python: your type checker sees only the assertions that fit the value, and every failure carries a structured diff. Soft assertions, polling, inline snapshots, OpenAPI contracts.
Actively maintained.Last release 2 weeks ago, commits in 26 of the last 26 weeks. A safe choice.
assert_that() uses @overload to return type-specific Protocols.
Your IDE shows only methods relevant to the value you're testing, not the whole surface:
An assertion hands the value back, statically narrowed.
is_not_none() strips None, is_instance_of() narrows to the class, and .value returns it:
order = assert_that(repo.find(42)).is_not_none().is_instance_of(PaidOrder).value
order.refund() # statically PaidOrder - verified by ty, mypy, pyright and pyrefly
For API tests,
assert_conforms()
validates a payload against a Pydantic model and narrows to it. exact=True catches contract drift:
data = assert_conforms(response.json(), OrderModel).value # data: OrderModel
Refinement predicates: satisfies() takes a TypeIs predicate, so a domain check narrows the chain too
Contract testing: assert_conforms() validates a raw payload against a Pydantic model and narrows to it. exact=True catches contract drift, each=True validates list endpoints
Soft assertions: thread-safe and async-safe via contextvars, each failure reported with its file:line. Group with sa.group() or assert_all()
Polling assertions: eventually() (async) / eventually_sync() (blocking) retry for eventual consistency, with a convergence trace on timeout
Expected exceptions: raises().when_called_with(), walk the cause chain (caused_by(), has_root_cause()), search an ExceptionGroup (contains_error(), errors(), error_of()), or pivot to the object (raised())
HTTP responses: assert on the response itself and every failure names the request it came from, with decoded_as_json() to step into the body. No client library is a dependency
OpenAPI response contracts: conforms_to_openapi() checks a JSON body against an operation's response schema, reporting every violation with its JSON path
Failure reporting
Structured errors: AssertionFailure carries .actual, .expected and .diff, and the diff renders into the message, so it shows off pytest too
Assertions as values: check() runs the next assertion for its verdict instead of raising, handing back an AssertionOutcome
Rich pytest diffs: recursive diffs across containers, dataclasses, attrs and Pydantic models, with intra-line carets for strings
Failures that say why: where one fact explains a failure, a line under the message says it. An id the payload holds as text (7 against "7"), a NaN, two instances compared by identity, surrounding whitespace. A failed contains() or is_in() names the closest row, and forty rows that differ in one field are named as one place
Plugin for pytest
Set from the command line or from [tool.pytest.ini_options], not from a call
Failure clustering: forty failing tests are usually not forty problems. Where three or more differ at the same place, the run ends with a line saying where. On by default, assertpy_failure_clusters = "off" turns it off
IDE comparison window: a failed is_equal_to() or has_<name>() gets PyCharm's "Click to see difference" link, as a plain assert does, and the window shows the two values it compared. Under ignore= or include= those go without the keys left out. The plugin hands them to pytest_assertrepr_compare, the hook the IDE's runner listens to. On by default, assertpy_comparison = "off" turns it off
Diagnostic profiles: assertpy_profile turns the guards below on in one line. compatible (default) leaves them off, safe warns, strict fails the tests they find. A setting you name yourself still wins
Vacuous-assertion guard: --assertpy-vacuous warns when a universal assertion passes over an empty collection, having checked nothing
Dangling-assertion detector: --assertpy-dangling warns when a chain builds an assertion and never runs it. assert assert_that(x).is_positive passes on any value, and neither ruff nor coverage sees it
Extensibility
Custom matchers: register_matcher() composes existing ones, BaseMatcher carries its own predicate. Both compose with &, |, ~
Allure (pip install assertpy[allure]): the pytest plugin auto-attaches structured diff and actual/expected data to Allure reports, in three configurable modes
Behave (pip install assertpy[behave]): ready-made parameter types (PositiveInt, NonEmptyString, ...) for step definitions like {age:PositiveInt}
Data frames (pip install assertpy[pandas] / [polars] / [numpy]): fluent equality for pandas/polars frames and numpy arrays, carrying each library's own diff