{"id":7151,"library":"delayed-assert","title":"Delayed/Soft Assertions for Python","description":"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.","status":"active","version":"0.4.2","language":"en","source_language":"en","source_url":"https://github.com/pr4bh4sh/python-delayed-assert","tags":["testing","assertions","soft-asserts","delayed-assertions","pytest","unittest"],"install":[{"cmd":"pip install delayed-assert","lang":"bash","label":"Install latest stable version"}],"dependencies":[],"imports":[{"note":"Evaluates an expression and records failures without stopping execution.","symbol":"expect","correct":"from delayed_assert import expect"},{"note":"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().","symbol":"assert_expectations","correct":"from delayed_assert import assert_expectations"},{"note":"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_'.","symbol":"test_case","correct":"from delayed_assert import test_case"},{"note":"Decorator or context manager that automatically calls 'assert_expectations()' at the end of the decorated function or 'with' block.","symbol":"assert_all","correct":"from delayed_assert import assert_all"},{"note":"Programmatically enables/disables the caller stack verification for 'expect()'.","symbol":"set_check_caller","correct":"from delayed_assert import set_check_caller"},{"note":"Programmatically enables/disables colorized output for failure reports.","symbol":"set_color_enabled","correct":"from delayed_assert import set_color_enabled"}],"quickstart":{"code":"from delayed_assert import expect, assert_expectations, test_case, assert_all\n\n@test_case\ndef my_test_scenario():\n    print(\"\\n--- Running my_test_scenario ---\")\n    expect(1 == 1, \"Assertion 1 (should pass)\")\n    expect(1 == 2, \"Assertion 2 (should fail)\")\n    expect(3 > 2, \"Assertion 3 (should pass)\")\n    expect(\"hello\" == \"world\", \"Assertion 4 (should fail)\")\n    print(\"Continuing after failed expectations...\")\n    assert_expectations()\n\n@assert_all()\ndef another_test_case():\n    print(\"\\n--- Running another_test_case (with decorator) ---\")\n    expect(True, \"True is True\")\n    expect(False, \"False is not True\")\n    # No need to call assert_expectations() explicitly here\n\nif __name__ == \"__main__\":\n    try:\n        my_test_scenario()\n    except AssertionError as e:\n        print(f\"Caught expected AssertionError for my_test_scenario:\\n{e}\")\n\n    try:\n        another_test_case()\n    except AssertionError as e:\n        print(f\"Caught expected AssertionError for another_test_case:\\n{e}\")\n","lang":"python","description":"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."},"warnings":[{"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)`.","message":"By 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.","severity":"gotcha","affected_versions":"0.4.1 and newer"},{"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))`.","message":"When 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`.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"If 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Disable color output by setting the environment variable `DELAYED_ASSERT_ENABLE_COLOR=0` or programmatically using `set_color_enabled(False)`.","message":"Colorized 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).","severity":"gotcha","affected_versions":"0.4.0 and newer"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"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).","cause":"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`).","error":"ImportError: No module named 'delayed_assert'"},{"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.","cause":"This error is the intended behavior of `assert_expectations()` (or `@assert_all()`) when one or more `expect()` calls have evaluated to `False`.","error":"AssertionError: Some expectations failed."},{"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))`).","cause":"Attempting to use Python's `assert` statement directly within a lambda function passed to `expect()`.","error":"SyntaxError: invalid syntax (cannot use 'assert' statement inside a lambda)"},{"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)`).","cause":"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.","error":"Error: expect() must be called from a method named 'test_*' or marked with '@test_case' if caller verification is enabled."}]}