Plugin enabling the use of exception instances with pytest.raises
Project description
pytest-raisin
Plugin putting a higher-level interface to pytest.raises. It allows to use an exception instance as the expected value, which would be compared with the actual exception (if any) based upon the type and the args attribute.
# Old-skool:
with pytest.raises(SystemExit) as cm:
sys.exit(1)
assert cm.value.args == (1,)
# New hotness:
with pytest.raises(SystemExit(1)):
sys.exit(1)
More sophisticated comparisons can be registered for user-defined error subclasses if necessary (see Advanced Usage).
Installation
pip install pytest-raisin
Basic Usage
Usage in your tests looks like this
>>> currant_exchange_rates = {
... "sultana": 50,
... "raisins": 100,
... }
>>> with pytest.raises(KeyError("grape")):
... currant_exchange_rates["grape"]
...
>>> with pytest.raises(KeyError("sultanas")):
... currant_exchange_rates["prunes"]
...
AssertionError: KeyError args do not match!
Actual: ('prunes',)
Expected: ('sultanas',)
>>> with pytest.raises(KeyError("Carlos Sultana")):
... currant_exchange_rates["sultana"]
Failed: DID NOT RAISE KeyError('Carlos Sultana')
The plugin is enabled by default: pytest.raises is monkeypatched with the new functionality directly. To temporarily execute without the new stuff, use pytest -p no:pytest-raisin.
The various legacy forms of pytest.raises will continue to work, falling back to the original implementation.
Advanced Usage
In most use-cases, the default behaviour of considering exceptions to be equivalent if the args attributes have matching tuples should be satisfactory. However, some 3rd-party exception classes have additional logic inside them (e.g. Django’s ValidationError) and you might want to provide a more custom assertion here.
Plugin users may register their own errors/callables via pytest-raisin’s decorator factory:
@pytest.register_exception_compare(MyError)
def my_error_compare(exc_actual, exc_expected):
...
Your comparison function will be called with the arguments exc_actual and exc_expected, which will both be directly instances of MyError (the test will have failed earlier if the type was not an exact match). This function should inspect the instances and raise an AssertionError with useful context message should they be considered not to match. It should do nothing (i.e. return None) if the exceptions should be considered equivalent.
Note: An instance of a subclass is not permitted when using an exception instance as the argument to pytest.raises. If you want to allow subclassing, use the original syntax of passing the type.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file pytest_raisin-0.2-py2.py3-none-any.whl
.
File metadata
- Download URL: pytest_raisin-0.2-py2.py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d94f2ca6e381a163097bba513396c60f812c2bccedb202a47f20566d7fe1da4b |
|
MD5 | 432271a0a85aa62e6ce87e70615d9c09 |
|
BLAKE2b-256 | 01d924180177db358bf18f44c07a579edceb1cacede66622cc65688ee8a835e2 |