pytest-maybe-raises
Pytest fixture for optional exception testing.
Documentation
I like to parametrize tests for functions that could accepts different values. I often find myself writing this type of pattern:
import pytest
@pytest.mark.parametrize(
('value', 'expected_result'),
(
('foo', 'bar'),
('baz', ValueError)
)
)
def test_something(value, expected_result):
if hasattr(expected_result, '__traceback__'): # is an exception
with pytest.raises(expected_result):
my_func(value)
else:
result = my_func(value)
assert result == expected_result
assert bool(result)
But this is really long and ugly.
Does pytest provides a fixture that allows me to only uses the pytest.raises
context when the provided argument is an exception class? The answer is not (see
this question in Stackoverflow),
so I've written this convenient fixture:
import contextlib
import pytest
@contextlib.contextmanager
def _maybe_raises(maybe_exception_class, *args, **kwargs):
if hasattr(maybe_exception_class, '__traceback__'):
with pytest.raises(maybe_exception_class, *args, **kwargs) as exc_info:
yield exc_info
else:
yield maybe_exception_class
@pytest.fixture()
def maybe_raises():
return _maybe_raises
This package provides a wrapper fixture over pytest.raises which
context only has effect if the passed argument is an exception class,
otherwise uses a null context like contextlib.nullcontext.
Using this package I can rewrite my tests as:
import pytest
@pytest.mark.parametrize(
('value', 'expected_result'),
(
('foo', 'bar'),
('baz', ValueError)
)
)
def test_something(value, expected_result, maybe_raises):
with maybe_raises(expected_result):
result = my_func(value)
assert result == expected_result
assert bool(result)
But in order to use it properly you need to know how the magic works:
Note that when an exception raises inside pytest.raises context, the
context exits so the later assert ... == expected_result comparison
is not executed. If other exception raises would be propagated to your test
so the comparison is not executed either. This allows you to write more
assertions after the execution of the function for successfull calls.
Metadata
Release files for pytest-maybe-raises 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pytest_maybe_raises-0.1.1.tar.gz | 4.0 kB | Without provenance | Details for pytest_maybe_raises-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytest_maybe_raises-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for pytest_maybe_raises-0.1.1-py3-none-any.whl |
Total release size: 8.4 kB
Release files / pytest_maybe_raises-0.1.1.tar.gz
| Download URL | pytest_maybe_raises-0.1.1.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e3ba96bc1a09b4d55066f6f337e1039ed12580e38cc79f49c947932a7021b5ce
|
|
BLAKE2b-256 checksum How to use checksums |
46a2f597b79fca83dacf76302714f47ebf07c4ef7632f1af17e206753a387227
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.13
|
Release files / pytest_maybe_raises-0.1.1-py3-none-any.whl
| Download URL | pytest_maybe_raises-0.1.1-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
11be1f64b965d9c2159a2169c9bf43d2573b9c66003478626702a988f5253b87
|
|
BLAKE2b-256 checksum How to use checksums |
e9ec04bbd779ceeb49c9a63cce72338826e0f7a05c3787564e72be3862068d86
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.13
|
Security for pytest-maybe-raises 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.