30

I'm quite new to pytest and I would like to know how to mark a test as "expected to fail" when called with certain parameters. I parametrize test like this:

@pytest.mark.parametrize("param1", [False, True])
@pytest.mark.parametrize("param2", [1, 2, 3])
def test_foo(self, param1, param2):
    ...

What I'm trying to achieve is that when the test is called with (param1 == True and param2 == 2), the test should fail; whilst any other parameter combinations should pass.

But I haven't found any way to do this. Do you have any ideas?

d4nyll
  • 11,811
  • 6
  • 54
  • 68
user1967718
  • 835
  • 1
  • 13
  • 22

1 Answers1

42

See xfail with parametrize:

@pytest.mark.parametrize("param2, param2", [
    (1, True),
    (2, True),
    pytest.param(1, False, marks=pytest.mark.xfail(reason='some bug')),
])
def test_foo(self, param1, param2):
    ...
Franz Forstmayr
  • 1,219
  • 1
  • 15
  • 31
Bruno Oliveira
  • 13,694
  • 5
  • 43
  • 41
  • FYI for others: I tried to use the `pytest.param` approach in the docs, but was getting "`param` not in module" - type of error. Not sure if I am outdated on my `pytest` version but something to try if you encounter this. The `pytest.mark.xfail` approach worked for me. – ximiki Jan 04 '18 at 20:25
  • You are right, probably you had an outdated pytest. `pytest.param` was introduced in `3.2`. – Bruno Oliveira Jan 05 '18 at 21:20
  • This doesn't seem to work in 3.1.2 ? I was able to put the xfail mark outside the parametrize, though (all of the cases were expected to fail.) – rschwieb Feb 22 '19 at 20:29
  • If I squint my eyes, I can see the answer trying to take form. When I read the actual text, I'm completely confused: why (`"param2, param2"` and not `"param2, param1"`? Why `pytest.param(1, False, ...` and not `pytest.param(2, True, ...`? When and how is param2 allowed to be 3? So many questions... – Michael Tiemann Jun 27 '23 at 13:10