pytest-raises

raw JSON →
0.11 verified Mon Apr 27 auth: no python maintenance

pytest-raises provides a `@pytest.mark.raises` decorator as an alternative to `pytest.raises` context manager. Current version 0.11 (no recent releases). Use is discouraged in favor of the built-in `pytest.raises` for new projects.

pip install pytest-raises
error AttributeError: module 'pytest' has no attribute 'mark'
cause pytest-raises plugin not installed or enabled.
fix
Install pytest-raises: pip install pytest-raises
error Failed: DID NOT RAISE <class 'ValueError'>
cause The test did not raise the expected exception.
fix
Ensure the test raises the exception, or use pytest.raises(ValueError) for better error context.
deprecated pytest-raises is no longer actively maintained. Prefer `pytest.raises` context manager or other mechanisms.
fix Rewrite using `with pytest.raises(ValueError): ...`
gotcha `@pytest.mark.raises` cannot use `pytest.param` or parametrization with the raises marker directly. The marker is applied to the whole function, not per-parametrization.
fix Use `pytest.raises` inside a parametrized test function.
gotcha If the test function does not raise the expected exception, the test fails with a custom error message from the plugin, not the usual `Failed: DID NOT RAISE` format. This can confuse CI log parsers.
fix Switch to built-in `pytest.raises` for consistent error messages.

Use @pytest.mark.raises to assert an exception is raised.

@pytest.mark.raises(ValueError, match='invalid')
def test_raises():
    raise ValueError('invalid value')