pytest.raises

A pytest helper, used in a with block, that makes a test pass only if the code inside raises the named exception.

In more detail

It is how you assert that something is refused. Its match argument is a regular expression checked against the error message; without it, the test would also pass if the same exception class were raised for an unrelated reason.

Example

with pytest.raises(KeyError, match="MOON"):
    rate_for("MOON")

Related terms

Taught in

References