To test exceptions with pytest, use pytest.raises as a context manager around the code that should fail: with pytest.raises(ValueError):. The test passes only if that exception type is raised inside the block, and fails if nothing is raised or a different exception appears. Testing that your code fails correctly is as important as testing that it succeeds.
This guide uses pytest 8 on Python 3.13, and assumes you can write a simple test function.
Take a function that should reject invalid input:
The basic test:
Only the code inside the with block is monitored. Keep that block to the single call that should raise, so an unrelated error elsewhere in the test cannot make it pass by accident.
The match argument checks the exception text against a regular expression using re.search:
Because match is a regular expression, special characters such as ., ( and [ need escaping. Use re.escape for literal text:
Use as excinfo to get an ExceptionInfo object when you need more than the type:
Read excinfo after the with block ends, not inside it. The useful attributes are:
| Attribute | Meaning |
|---|---|
excinfo.type | The exception class |
excinfo.value | The exception instance, so you can check custom fields |
excinfo.traceback | The traceback entries |
For exceptions with their own attributes, assert on the field rather than the string, for example excinfo.value.code == 404.
Pass a tuple when more than one exception type is acceptable:
pytest.raises also matches subclasses, so pytest.raises(Exception) passes for almost anything. That is too broad to be a useful test. Name the most specific exception your code is documented to raise.
Python 3.11 added exception groups, and pytest 8.4 added pytest.RaisesGroup for them. For an ordinary ExceptionGroup, you can also check the contents with excinfo.group_contains. A chained error keeps its cause available:
The __cause__ attribute is set by raise ... from, which is how you verify that the original problem was not lost.
There is no pytest.raises for the happy path. Call the code normally, and if it raises, the test fails with a full traceback.
Avoid wrapping it in try/except just to call pytest.fail, because that hides the real traceback. To assert on the successful result, as above, is the clearest option.
Warnings use a sibling helper:
When many bad inputs should all raise, combine raises with parametrize:
Exception instead of the specific type.match is a regular expression.excinfo inside the with block, where it is not filled yet.pytest.raises(..., message=...) argument, which was removed; use match instead.pytest fails the test with DID NOT RAISE and the expected type, so a function that silently accepts bad input is caught.
Yes. An exception that is a subclass of the expected type satisfies it, so choose a specific type.
Capture it with as excinfo and assert on excinfo.value, for example excinfo.value.code == 404, after the with block.
It works inside pytest, but pytest.raises has match, richer failure output and plain function style, so it is the usual choice.
Wrap only the failing call in with pytest.raises(SomeError):, and add match to check the message with a regular expression. Capture as excinfo to examine type, value or __cause__ after the block, use a tuple for several acceptable types, and combine with parametrize to cover many invalid inputs. Use pytest.warns for warnings, and keep the expected exception as specific as possible.