asserts
raw JSON → 0.14.0 verified Mon Apr 27 auth: no python
A standalone assertions library providing a rich set of assertion functions beyond unittest, with support for JSON subset checking, datetime assertions, regex, and context managers. Current version 0.14.0, requires Python >=3.10, released irregularly.
pip install asserts Common errors
error ModuleNotFoundError: No module named 'asserts' ↓
cause Package not installed.
fix
Run 'pip install asserts'.
error TypeError: 'AssertRaisesContext' object is not callable ↓
cause Using assert_raises as a decorator instead of context manager.
fix
Use with assert_raises(ExcType): ...
error AttributeError: module 'asserts' has no attribute 'Exists' ↓
cause Exists was renamed to Present in 0.13.0.
fix
Use 'from asserts import Present' and replace Exists with Present.
Warnings
breaking Python 3.8 and 3.9 support dropped in 0.14.0. Requires Python >=3.10. ↓
fix Upgrade to Python 3.10+ or pin asserts<0.14.0.
deprecated Exists is deprecated since 0.13.0; use Present or Absent instead. ↓
fix Replace Exists with Present for existence or Absent for non-existence.
gotcha assert_raises and assert_warns are context managers, not decorators. Must use 'with' statement. ↓
fix Use 'with assert_raises(Exception):' pattern.
breaking assert_datetime_about_now and assert_datetime_about_now_utc now raise AssertionError instead of TypeError for timezone-aware datetimes. ↓
fix Ensure datetime arguments are naive or adjust exception handling.
Imports
- assert_equals wrong
from asserts.asserts import assert_equalscorrectfrom asserts import assert_equals - assert_raises wrong
from asserts import assert_raises as * or from unittest import assertRaisescorrectfrom asserts import assert_raises - Present wrong
from asserts import Existscorrectfrom asserts import Present - assert_json_subset
from asserts import assert_json_subset
Quickstart
from asserts import assert_equals, assert_raises
def test_addition():
assert_equals(2 + 2, 4)
with assert_raises(ValueError):
int('abc')