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-assertVerified import paths — ran on the pinned version, not inferred.
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.
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)`.
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))`.
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.
Disable color output by setting the environment variable `DELAYED_ASSERT_ENABLE_COLOR=0` or programmatically using `set_color_enabled(False)`.
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).
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.
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))`).
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)`).
No dependency data recorded yet.