I have some parametrized tests
def test1():
#do test1
def test2():
#do test2
def test3():
#do test3
Each test is parametrized by
@pytest.mark.parametrize(x)
I would like to run these tests against
test_data=[1,2,3,4]
I have tried using pytest-depends
@pytest.mark.depends(on=['test1'])
@pytest.mark.parametrize(x)
However, I get that all test_2 are skipped if any test_1 fails. Instead, I would like test_2 for the specific parametrization to be skipped only if test_1 failed for the specific parametrization.
Can this be obtained in pytest?
This is possible, if you add separate dependencies for each parameter. In your example you could do:
In this case,
test2[2]
will be skipped, becausetest1[2]
fails.If you want to take the test data from a variable or function, or you don't want that much clutter in the
parametrize
decorator, you can also do this in a bit more generic way:In this case
test2[2]
will be skipped as before becausetest1[2]
fails,test3[2]
will be skipped becausetest2[2]
fails, andtest3[3]
will be skipped because of the failingtest2[3]
.This of course works only for one argument. If you have more than one argument, as mentioned in the comments, you have to adapt the code accordingly:
or in the more generic version: