How do you assert that code raises a ValueError in pytest?
Options
assert raises(ValueError)placed after the callwith pytest.raises(ValueError): parse('x')(Correct option)try/exceptfollowed byassert True@pytest.mark.errorabove the test
Answer
with pytest.raises(ValueError): parse("x")
Why
The test passes only if the code inside the with block raises ValueError; if no error happens, the test fails.
You can also check the message: pytest.raises(ValueError, match="invalid").
A hand-written try/except that does nothing when no error occurs would pass silently and hide bugs.
Remember
Expect an error → with pytest.raises(...).