1

为什么跳过第二次测试?我希望第二个测试依赖于三个参数化为 test_first 的测试。如何让它发生?

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)

输出是

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

我想t.py::test_second PASSED

ps 可能是之前问过的,但我还是决定发布这个问题,因为很难找到关于这个问题的简要表述的问题。

4

3 回答 3

2

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
于 2020-01-27T09:47:15.780 回答
0

好吧,我对 pytest-dependency 的工作原理一无所知,但通常参数化的测试是表示/命名的,包括它们的参数值,例如在内部test_first[10]并且test_first[20]是不同的测试,也许试试?查看它间接暗示的文档,请注意instances帮助程序如何生成表单的名称$testname[$params...]

该文档还谈到(建议?)显式标记参数化实例:https ://pytest-dependency.readthedocs.io/en/latest/usage.html#parametrized-tests

于 2020-01-27T09:27:11.483 回答
0

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)
于 2020-01-27T09:47:16.043 回答