'How to decorate a test to xfail a specific case with multiple parametrization?
Is there a way in pytest to xfail a test when a specific parametrization condition is met?
Note the parametrization is on fixture, not using mark.parametrize.
from pytest import fixture, xfail
@fixture(params=['a', 'b'])
def setup_1(request):
return request.param
@fixture(params=['x', 'y'])
def setup_2(request):
return request.param
@xfail() # Something here ??
def test_something(setup_1, setup_2):
... # Asserrt something
I would like to modify the example above so the test is xfail when, for example setup_1='a' and setup_2='x'?
I know one option according to pytest documentation is to just use a condition inside the test:
def test_something(setup_1, setup_2):
if setup_1 == 'a' and setup_2 == 'x':
pytest.xfail("failing configuration (but should work)")
else:
... # Assert something
However, I was wondering if there might be a more readable way using decorators.
Solution 1:[1]
I think that is not possible to do easily what you intend to do with parameters coming from multiple parametrized fixtures. However and even if it's not exactly what you intend to do, it's worth highlighting that you can parametrize xfail as stated in the documentation.
@pytest.mark.parametrize(
("setup_1", "setup_2"), [
pytest.param("a", "x", marks=pytest.mark.xfail(reason="some bug")),
("a", "y"),
("b", "x"),
("b", "y"),
]
)
def test_something(setup_1, setup_2):
print(f"{setup_1}, {setup_2}")
Unfortunately and as far as I know, it does not work with parametrized fixtures. So in conclusion, if you need to have parameters coming from fixtures, your option (condition in the test method) seems to be the best solution.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
