pytest.raises
noun
say it: “PY-test dot RAY-ziz” /ˈpaɪtɛst dɑt ˈreɪzɪz/
A pytest tool, used in a with block, that makes a test pass only when the code inside it raises the error you name.
What does pytest.raises do?
pytest.raises checks that code raises an error. Put the call inside with pytest.raises(...): the test passes only
if that error is raised, and match= can check its message.
import pytest
def test_negative_weight_is_refused():
with pytest.raises(ValueError, match="weight"):
shipping_cost(-2)
In a sentence
- “The test uses pytest.raises to prove a negative weight is refused.”
- “Without pytest.raises, the ValueError would fail the test instead of passing it.”
Level: Journeyman · More Python terms at this level
Updated