pytest-assume
pytest-assume is a pytest plugin that enhances test assertion behavior by allowing multiple failures within a single test. Instead of stopping test execution on the first failed assertion, it collects all failures reported via `pytest.assume()` and reports them at the end of the test, providing a comprehensive view of all issues. The current version is 2.4.3, with a regular release cadence addressing bug fixes and compatibility improvements.
Warnings
- gotcha Unlike standard Python `assert` statements, `pytest.assume()` does not immediately halt test execution upon failure. Its core purpose is to allow tests to continue running and collect all failures before reporting them at the end. This can be a surprise for users expecting traditional assertion behavior.
- gotcha pytest-assume has historically had nuanced interactions with other pytest plugins, particularly those that modify hook execution order or capture output (e.g., log capturing). While recent versions (e.g., 2.4.3) include fixes like `tryfirst=True` for specific hooks, conflicts can still arise. Previous versions also saw changes to internal pytest hooks used (e.g., 2.1.0, 2.4.0).
- gotcha When using `pytest-assume` with `xfail` markers, there have been bug fixes in how `xfail` conditions are handled in conjunction with multiple assumptions. While version 2.4.1 addressed specific bugs, unexpected interactions might still occur with complex `xfail` configurations.
Install
-
pip install pytest-assume
Imports
- pytest.assume
import pytest # ... use pytest.assume()
Quickstart
import pytest
def test_multiple_conditions():
# These assumptions will all be evaluated,
# and all failures reported at the end of the test.
pytest.assume(1 == 1, "This assumption should pass")
pytest.assume(2 == 3, "Expected 2 to be 3, but it was not!")
pytest.assume("hello" == "hello", "String comparison should pass")
pytest.assume(5 > 10, "5 is not greater than 10")
# Using the context manager for a group of assumptions
with pytest.assume:
pytest.assume(True, "Context manager assumption 1 (pass)")
pytest.assume(False, "Context manager assumption 2 (fail)")
# A standard assert would halt the test here if it failed,
# but pytest.assume allows execution to continue.
assert 1 == 1 # This line is reached even if pytest.assume() calls failed