You have a test that’s parametrized but only one of the examples fails. While it seems you may have run into an edge case, you don’t have time to fix the code but need to get the test to stop failing. (And it goes without saying that you can’t remove that example either!) One way to do this is mark that specific as xfail
. But how do you do that for only the one example?
First, let’s see the original test
from pytest import mark
@mark.parametrize(
"add_value, expected",
[
(3, 6),
(4, 7),
(-1, -4), # This one (obviously) fails!
],
ids=["add 3", "add 4", "add -1"])
def test_add_3(add_value, expected):
assert add_value + 3 == expected
Let’s set aside that the third example above is just plain wrong–we just needed an example that fails. Now let’s modify the test so that the entire test will stop failing.
from pytest import mark, param
@mark.parametrize(
"add_value, expected",
[
param(3, 6),
param(4, 7),
param(-1, -4, marks=mark.xfail(raises=AssertionError, reason="Doesn't add up!")),
],
ids=["add 3", "add 4", "add -1"])
def test_add_3(add_value, expected):
assert add_value + 3 == expected
When running the new test, it will return 2 PASS 1 XFAIL. Also worth mentioning that the raises=
& reason=
keywords are both entirely optional, however, I’d make the case that they do leave a valuable “crumb trail” as to why/how the test is failing. That way when you come back to figuring out why that test is failing with that example, you at least have an idea as to what might be the cause!