AssertPy

1.1 · active · verified Sun Apr 12

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.

Warnings

Install

Imports

Quickstart

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()

view raw JSON →