Skip to main content

pypi pyversions actions

https://user-images.githubusercontent.com/6615374/50065259-46af2780-017b-11e9-8af3-38f340f11df1.png

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.

Metadata

Release files for pytest-raisin 0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-raisin 0.4
File Size Uploaded
pytest-raisin-0.4.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-raisin 0.4
File Interpreter ABI Platform
pytest_raisin-0.4-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 8.3 kB

Release files / pytest-raisin-0.4.tar.gz

Download URL pytest-raisin-0.4.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
9a247cb8a22d8bc4c23b060bea7b9d5cff1d83b828e59fcfd5779b371078fc8f
BLAKE2b-256 checksum
How to use checksums
06d2b5cb736db9bb5afadfef53cbe64c23f34570f6ba540b2e82357b324ed49c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1

Release files / pytest_raisin-0.4-py2.py3-none-any.whl

Download URL pytest_raisin-0.4-py2.py3-none-any.whl
Size 4.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b58b3c9258f07478e1b21ee074669ed66000e2633126b088edfa3cf0db8b60c0
BLAKE2b-256 checksum
How to use checksums
ebac0119ea187d94a716b40dbe6de66c3b48c9f87e9c3b080913a15076a8ee62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1

Release history Release notifications | RSS feed

This release

0.4 This release

2 release files

0.3

2 release files

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page