Decorators for pytest tests that should issue exceptions or warnings
Project description
pytest-issues
A decorator for testing exceptions with pytest.
About
The pytest-issues package provides decorators to ensure that pytest tests raise expected exceptions and warnings. Use the @raises decorator to check a test raises an expected exception, and @warns to check a test issues an expected warning. The decorators are built on the pytest.raises and pytest.warns context managers, and extends their functionality to allow validation of error messages using strings. (The baseline context managers require regular expressions to validate error messages).
Some motivations for the package include:
- Testing error messages without needing regular expressions
- Adding a marker to quickly identify tests that examine error states
- Keeping the action code for exception tests at the same indent level as other tests
Examples
Require a test to raise an expected exception:
from pytest_issues import raises
@raises(ValueError, "some message A", "another message B", "final message C")
def test():
raise ValueError(
"This test must raise an ValueError whose error message "
"must include some message A, another message B, and a final message C."
)
@raises((TypeError, ValueError), 'some message')
def test_multiple_types():
raise TypeError(
"This test must raise either a TypeError or ValueError, "
"and the error must include some message".
)
Auto-fail and display the raised message in the pytest results (useful for examining error messages when writing tests):
@raises(RuntimeError)
def test():
raise RuntimeError(
"Because @raises does not include any error message strings, "
"this test will fail and will display the raised error message "
"in the pytest output. Refer to the documentation for ways to "
"disable auto-failing."
)
Plays well with the pytest ecosystem:
@raises(TypeError, 'some error message')
@pytest.mark.parametrize('parameter', (1,2,3))
def test_with_parameters(parameter):
raise TypeError(
'This test uses parameters, and should raise a TypeError with some error message'
)
@raises(ValueError, 'some error message')
def test_with_fixtures(fixtureA, fixtureB):
raise ValueError(
'This test uses fixtures, and should raise a ValueError with some error message'
)
class TestClass:
@raises(RuntimeError, 'some error message')
def test_that_is_a_method(cls):
raise RuntimeError(
'This test is a class method, and should raise a RuntimeError with some error message'
)
@raises((ValueError, TypeError), 'some message', check=some_callable, match=some_regex)
def test_raises_api():
raise ValueError(
"This exception raised by this test must satisfy the `check` and `match` args "
"from the pytest.raises API, and must raise a ValueError with some message."
)
Inject parameters and fixtures into expected error messages:
@raises(ValueError, 'message including {param1} and {fixture2}')
@pytest.mark.parametrize(
'param1', ('some message A', 'some message B')
)
def test(param1, fixture1, fixture2):
raise ValueError(
f'This test should raise an exception that has a message '
f'including {param1} and {fixture2}'
)
Check for warning messages:
import warnings
from pytest_issues import warns
@warns(RuntimeWarning, 'some warning message')
def test():
warnings.warn(
"This test must issue a RuntimeWarning that includes "
"some warning message."
)
Documentation
You can find more detailed examples, a user guide, and a comprehensive API at the project documentation.
Installation
Requires: Python 3.11+
Dependencies: pytest 8.4+
pip install pytest-issues
Contributing / Feedback
We welcome contributions and feedback! To ask a question, suggest a feature, or report a bug, please open a new thread on our Issues tracker. If you plan to contribute code, please read the contribution guide in the docs.
License
This project is licensed under the MIT license. Refer to the license file for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_issues-1.0.1.tar.gz.
File metadata
- Download URL: pytest_issues-1.0.1.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcf25d1baf06765be55c44d08c2abff72891a1aca504b4c6b0da8cf8a2a86b11
|
|
| MD5 |
3e8364a282e987a97637909b9c7c038c
|
|
| BLAKE2b-256 |
8e86360bac103021ec6c0f03c9d3fce137ef405a9227c0ce6ef47129ae85a2a8
|
File details
Details for the file pytest_issues-1.0.1-py3-none-any.whl.
File metadata
- Download URL: pytest_issues-1.0.1-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
140a6e76d1f32b03ae55bb251cce34f4e879b8ce9c7c069ff398e007d147d349
|
|
| MD5 |
f41a90cacb9bcd0102ff940c09388e54
|
|
| BLAKE2b-256 |
bc3ee86d591df0eb0286e92068e7a6b80eb852cd642f230560f1f2c302d6b231
|