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
- exception — An error object a program raises to stop normal flow and report that something went wrong.
- context manager — An object that sets something up and later tears it down, designed to be used with a with statement.
- regular expression — A pattern language for matching text, where characters such as ., *, ^ and $ have special meanings.
Taught in
- Introducing pytest — Automated Testing, TDD & Quality Engineering
- Making Failures Explain Themselves — Automated Testing, TDD & Quality Engineering