Registry / testing / assertpy

assertpy

JSON →
library1.1pypypi✓ verified 22d ago

AssertPy is a simple assertion library for Python unit testing with a fluent API, inspired by AssertJ for Java. It allows for highly readable and chainable assertions. The current version is 1.1, and it has an active release cadence with frequent updates.

pip install assertpy
INSTALL
IMPORT
SIG · ASSERTPY
A
assertpy
testingpythonv1.1
Install
2.6s avg
Import
55ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.056s · 19.5MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.6s · import 0.054s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

assert_that
from assertpy import assert_that
The primary entry point for all fluent assertions.
soft_assertions
from assertpy import soft_assertions
Used as a context manager to collect multiple assertion failures without halting execution immediately.
assert_warn
from assertpy import assert_warn
from assertpy import assert_soft
Renamed from `assert_soft` in v0.10; logs warnings instead of raising an AssertionError.

This quickstart demonstrates basic chained assertions using `assert_that` and shows how to use `soft_assertions` as a context manager to collect multiple failures before raising a single `AssertionError` at the end of the block.

from assertpy import assert_that, soft_assertions def test_basic_assertions(): assert_that(1 + 2).is_equal_to(3) assert_that('foobar').is_length(6).starts_with('foo').ends_with('bar') assert_that(['a', 'b', 'c']).contains('a').does_not_contain('x') print('Basic assertions passed.') def test_soft_assertions(): with soft_assertions(): assert_that(1).is_equal_to(2) # This will fail assert_that('hello').is_length(10) # This will also fail print('Soft assertions completed. Failures (if any) are collected.') test_basic_assertions() test_soft_assertions()
Debug
Known issues
breakingThe `assert_soft()` function was renamed to `assert_warn()`.
fix
Update calls from `assert_soft()` to `assert_warn()`.
affects: v0.10 and later
breakingThe `extract()` helper function was renamed to `extracting()`.
fix
Change all uses of `extract()` to `extracting()`.
affects: v0.9 and later
gotchaOlder versions of AssertPy supported Python 2.x, but modern versions are Python 3.x only. Specific older Python 3 versions (e.g., 3.3, 2.6) have also been dropped over time.
fix
Ensure you are using a Python 3 environment (Python 3.4-3.8 for v1.1) for full compatibility. Check `assertpy`'s PyPI classifiers for specific version support.
affects: <=v0.13 (Python 2.6 dropped), <=v0.9 (Python 3.3 dropped)
gotchaWhen using `matches()` for regular expression assertions, be aware that it performs a partial match (like `re.match`). For exact full string matches, include anchors (e.g., `^...$`) in your regex pattern.
fix
Use raw strings (`r'pattern'`) for regex patterns and include `^` and `$` anchors for full string matching if desired.
affects: All versions
gotchaFor `dict` equality comparisons with `is_equal_to()`, you can control which keys are included or ignored. If not specified, all keys are compared.
fix
Utilize the `include` (or `ignore`) keyword argument in `is_equal_to(expected_dict, include=['key1', 'key2'])` for precise dict comparisons.
affects: v0.14 and later (for `include` flag)
gotchaSnapshot testing (`.snapshot()`) requires Python 3.x and saves artifacts to disk (e.g., `__snapshots` directory by default). These artifacts should be committed to version control.
fix
Ensure Python 3 is used for snapshot tests and add the `__snapshots` directory (or custom path) to your version control.
affects: v0.13 and later
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'assertpy'
The 'assertpy' library is not installed in the Python environment.
fix
Install the library using pip: 'pip install assertpy'.
AttributeError: module 'assertpy' has no attribute 'assert_that'
Incorrect import statement; 'assert_that' should be imported directly from 'assertpy'.
fix
Use the correct import: 'from assertpy import assert_that'.
AttributeError: 'AssertionBuilder' object has no attribute 'foo'
Attempting to use a non-existent assertion method 'foo' with 'assert_that'.
fix
Ensure that the assertion method exists in 'assertpy' and is correctly spelled.
AssertionError: Expected attribute <foo>, but val has no attribute <foo>.
Using a dynamic assertion method like 'has_foo()' on an object that lacks the 'foo' attribute.
fix
Verify that the object has the specified attribute before using dynamic assertions.
TypeError: assertion <has_first_name()> takes exactly 1 argument (0 given)
Calling a dynamic assertion method without the required argument.
fix
Provide the expected value as an argument: 'assert_that(obj).has_first_name('ExpectedName')'.
Upgrade
Version history
1.1latest on PyPI · released Jul 19, 2020
Audit
Dependencies

No dependency data recorded yet.

Agent activity
32 hits · last 30 days
node
26
OpenAI (training)
1
Resources
assertpy — pip install assertpy · libregistry