Skip to main content

An implementation of pytest.raises as a pytest.mark fixture

Project description


[![Current Build Status](](

A [pytest][] plugin implementation of pytest.raises as a pytest.mark fixture


Adds functionality for marking tests with a `pytest.mark.raises` fixture, which functions similarly to using `with pytest.raises`


- python 2.7 or above
- pytest 2.8.1 or above


You can install "pytest-raises" via [pip][] from [PyPI][]

$ pip install pytest-raises


Marking a test with the `@pytest.mark.raises()` decorator will mark that the code the test executes is expected to raise an error. This is different from `@pytest.mark.xfail()` as it does not mean the test itself might fail, but instead that the "pass" for the test is that the code raises an error.

It accepts an `exception` keyword argument, which is the class of error expected to be raised.

It will allow tests which raise errors to pass. The main usage is to assert that an error of a specific type is raise.

A very simple example is:

import pytest

class SomeException(Exception):

class AnotherException(Exception):

@pytest.mark.raises(exception = SomeException)
def test_mark_raises_named():
raise SomeException('the message')

def test_mark_raises_general():
raise AnotherException('the message')


A more useful example using test parametrization is:

import pytest

class SomeException(Exception):

class AnotherException(Exception):

@pytest.mark.parametrize('error', [
pytest.mark.raises(SomeException('the message'), exception=SomeException),
pytest.mark.raises(AnotherException('the message'), exception=AnotherException),
pytest.mark.raises(Exception('the message')),
def test_mark_raises(error):
if error:
raise error


All of these tests pass. These examples are actual [tests for this plugin][].


Distributed under the terms of the [MIT][] license, "pytest-raises" is free and open source software


If you encounter any problems, please [file an issue][] along with a detailed description.

[file an issue]:
[tests for this plugin]:

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pytest-raises, version 0.10
Filename, size File type Python version Upload date Hashes
Filename, size pytest-raises-0.10.tar.gz (7.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page