Delayed/Soft Assertions for Python

0.4.2 · active · verified Thu Apr 16

delayed-assert is a Python library that provides delayed or soft assertions, allowing test execution to continue even after an `expect()` call fails. All accumulated failures are then reported at a designated point using `assert_expectations()` or implicitly with a decorator/context manager. It is framework-agnostic and currently at version 0.4.2, with a moderate release cadence focusing on usability and feature enhancements like improved test case identification and output control.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core functionality of delayed-assert. The `my_test_scenario` function shows explicit use of `expect()` and `assert_expectations()`. The `another_test_case` function demonstrates the `@assert_all()` decorator, which automatically collects and asserts all expectations at the end of the function. Failed expectations do not halt execution immediately, but `assert_expectations()` (or `@assert_all()`) will raise an `AssertionError` at the end if any `expect()` calls failed.

from delayed_assert import expect, assert_expectations, test_case, assert_all

@test_case
def my_test_scenario():
    print("\n--- Running my_test_scenario ---")
    expect(1 == 1, "Assertion 1 (should pass)")
    expect(1 == 2, "Assertion 2 (should fail)")
    expect(3 > 2, "Assertion 3 (should pass)")
    expect("hello" == "world", "Assertion 4 (should fail)")
    print("Continuing after failed expectations...")
    assert_expectations()

@assert_all()
def another_test_case():
    print("\n--- Running another_test_case (with decorator) ---")
    expect(True, "True is True")
    expect(False, "False is not True")
    # No need to call assert_expectations() explicitly here

if __name__ == "__main__":
    try:
        my_test_scenario()
    except AssertionError as e:
        print(f"Caught expected AssertionError for my_test_scenario:\n{e}")

    try:
        another_test_case()
    except AssertionError as e:
        print(f"Caught expected AssertionError for another_test_case:\n{e}")

view raw JSON →