Registry / testing / delayed-assert

delayed-assert

JSON →
library0.4.2pypypi✓ verified 86d ago

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.

pip install delayed-assert
INSTALL
IMPORT
SIG · DELAYED-ASSERT
D
delayed-assert
testingpythonv0.4.2
Install
1.5s avg
Import
27ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.2 · 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.920 runs
installs and imports cleanly · install 0.0s · import 0.027s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.5s · import 0.027s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

expect
from delayed_assert import expect
Evaluates an expression and records failures without stopping execution.
assert_expectations
from delayed_assert import assert_expectations
Raises an AssertionError if any 'expect()' calls failed in the current scope. Must be called explicitly if not using @assert_all or with assert_all().
test_case
from delayed_assert import test_case
Decorator to explicitly mark a function as a test case, allowing 'expect()' to work correctly even if the function name doesn't start with 'test_'.
assert_all
from delayed_assert import assert_all
Decorator or context manager that automatically calls 'assert_expectations()' at the end of the decorated function or 'with' block.
set_check_caller
from delayed_assert import set_check_caller
Programmatically enables/disables the caller stack verification for 'expect()'.
set_color_enabled
from delayed_assert import set_color_enabled
Programmatically enables/disables colorized output for failure reports.

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}")
Debug
Known issues
gotchaBy default, `expect()` verifies that it is called from a method named `test_something`. If you use `expect()` in helper methods or functions not prefixed with `test_`, it might raise an error.
fix
Use the `@test_case` decorator on your test function, or disable caller verification globally by setting the environment variable `DELAYED_ASSERT_CHECK_CALLER=0`, or programmatically with `set_check_caller(False)`.
affects: 0.4.1 and newer
gotchaWhen using `expect()` with lambda expressions, `assert` statements are not valid within a lambda. For example, `expect(lambda: assert 1 == 1)` will result in a `SyntaxError`.
fix
Pass an expression directly (e.g., `expect(1 == 1)`) or, if integrating with `unittest` assertions, pass the assertion call as a lambda expression without the `assert` keyword, e.g., `expect(lambda: self.assertEqual(3, 4))`.
affects: All versions
gotchaIf you are not using the `@assert_all()` decorator or `with assert_all():` context manager, you must explicitly call `assert_expectations()` at the end of your test case to report any collected failures.
fix
Ensure `assert_expectations()` is called at the logical end of your test method where you want failures to be aggregated and reported. Alternatively, wrap your test method with `@assert_all()` or its contents in a `with assert_all():` block.
affects: All versions
gotchaColorized output for failure reports is enabled by default. This might introduce unwanted ANSI escape codes into logs or environments that don't support color output (e.g., some CI/CD pipelines).
fix
Disable color output by setting the environment variable `DELAYED_ASSERT_ENABLE_COLOR=0` or programmatically using `set_color_enabled(False)`.
affects: 0.4.0 and newer
Errors
Common errors & fixes
ImportError: No module named 'delayed_assert'
The `delayed-assert` package is either not installed, or there's an issue with the Python environment's path, or an incorrect import statement (e.g., `import delayed_assert.delayed_assert`).
fix
Ensure the package is installed using `pip install delayed-assert`. Verify the import statement is `from delayed_assert import expect, assert_expectations` (or other specific symbols).
AssertionError: Some expectations failed.
This error is the intended behavior of `assert_expectations()` (or `@assert_all()`) when one or more `expect()` calls have evaluated to `False`.
fix
Review the details printed with the `AssertionError` to identify which `expect()` calls failed. These details will include the expression, message, and location of the failure. This indicates a test failure, not a library issue.
SyntaxError: invalid syntax (cannot use 'assert' statement inside a lambda)
Attempting to use Python's `assert` statement directly within a lambda function passed to `expect()`.
fix
Reformulate the condition as a boolean expression (e.g., `expect(x == y)`) or, if integrating with `unittest` assertions, pass the `unittest` assertion method directly to the lambda (e.g., `expect(lambda: self.assertEqual(x, y))`).
Error: expect() must be called from a method named 'test_*' or marked with '@test_case' if caller verification is enabled.
This occurs when `expect()` is called from a function whose name does not start with `test_`, and caller verification (default enabled in >=0.4.1) is active.
fix
Rename your function to start with `test_`, apply the `@test_case` decorator to the function, or disable caller verification as described in the warnings section (e.g., `DELAYED_ASSERT_CHECK_CALLER=0` or `set_check_caller(False)`).
Upgrade
Version history
0.4.2latest on PyPI · released Dec 7, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
12
Resources
delayed-assert — pip install delayed-assert · libregistry