pythonpython-3.xtestingpytestpytest-dependency

How to make test that depends on parametrized test. (Pytest)


Why second test is skipped? I want second test to depend on three tests which are parametrized as test_first. How to make it happen?

import pytest
from pytest_dependency import depends
param = [10,20,30]
@pytest.mark.parametrize("param", param)
def test_first(param):
    assert(True)

@pytest.mark.dependency(depends=['test_first'])
def test_second():
    assert(True)

Output is

t.py::test_first[10] PASSED
t.py::test_first[20] PASSED
t.py::test_first[30] PASSED
t.py::test_second SKIPPED

I want t.py::test_second PASSED

p.s. It may be to asked before, but I decided to post the question anyway, because it is hard to find briefly formulated question about this problem.


Solution

  • From this example I can see that (1) you also should decorate test_first and (2) decorate the parameter list.

    
    # The test for the parent shall depend on the test of all its children.
    # Create enriched parameter lists, decorated with the dependency marker.
    
    childparam = [ 
        pytest.param(c, marks=pytest.mark.dependency(name="test_child[%s]" % c)) 
        for c in childs
    ]
    parentparam = [
        pytest.param(p, marks=pytest.mark.dependency(
            name="test_parent[%s]" % p, 
            depends=["test_child[%s]" % c for c in p.children]
        )) for p in parents
    ]
    
    @pytest.mark.parametrize("c", childparam)
    def test_child(c):
        if c.name == "l":
            pytest.xfail("deliberate fail")
            assert False
    
    @pytest.mark.parametrize("p", parentparam)
    def test_parent(p):
        pass