g_d
g_d

Reputation: 707

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.

Upvotes: 1

Views: 1805

Answers (3)

Harion
Harion

Reputation: 225

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

Upvotes: 2

g_d
g_d

Reputation: 707

One of possible solutions to my question is the code below, but it ruins the independency of parametrized tests... So I am still interested in another better solution.

import pytest
from pytest_dependency import depends
param = [10,20,30]

@pytest.mark.dependency(name="a1")
def test_dum():
    pass

@pytest.mark.parametrize("param", param)
@pytest.mark.dependency(name="a1", depends=['a1'])
def test_first(param):
    assert((param == 10) or (param == 20) or (param == 31))

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

Upvotes: 0

Masklinn
Masklinn

Reputation: 42302

Well I don't know anything about how pytest-dependency works, but generally parametrized tests are represented / named including their parameter value e.g. internally test_first[10] and test_first[20] are different tests, maybe try that? Looking over the documentation it obliquely hints at that being the case, note how the instances helper generates names of the form $testname[$params...].

The documentation also talks about (suggests?) explicitly marking parametrized instances: https://pytest-dependency.readthedocs.io/en/latest/usage.html#parametrized-tests

Upvotes: 0

Related Questions