Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark test as xfail only with specific parameters with Pytest

Tags:

testing

pytest

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?

like image 903
user1967718 Avatar asked Jun 01 '15 10:06

user1967718


People also ask

How do you mark a test in pytest?

Custom marker and command line option to control test runs. The --markers option always gives you a list of available markers: $ pytest --markers @pytest. mark.

What is the syntax to run a parameterized test in pytest?

you can put @pytest. mark. parametrize style parametrization on the test functions to parametrize input/output values as well.

Can pytest fixtures be parameterized?

pytest. fixture() allows one to parametrize fixture functions.


1 Answers

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):
    ...
like image 192
Bruno Oliveira Avatar answered Sep 20 '22 09:09

Bruno Oliveira