简体   繁体   English

如何进行依赖于参数化测试的测试。 (pytest)

[英]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.我希望第二个测试依赖于参数化为 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我想要t.py::test_second PASSED

ps 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. ps 之前可能会问,但我还是决定发布这个问题,因为很难找到关于这个问题的简要制定的问题。

From this example I can see that (1) you also should decorate test_first and (2) decorate the parameter list.从这个 例子我可以看到(1)你还应该装饰 test_first 和(2)装饰参数列表。


# 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

Well I don't know anything about how pytest-dependency works, but generally parametrized tests are represented / named including their parameter value eg internally test_first[10] and test_first[20] are different tests, maybe try that?好吧,我对 pytest-dependency 的工作原理一无所知,但通常参数化测试的表示/命名包括它们的参数值,例如内部test_first[10]test_first[20]是不同的测试,也许试试看? Looking over the documentation it obliquely hints at that being the case , note how the instances helper generates names of the form $testname[$params...] .查看文档, 它暗示是这种情况,请注意instances助手如何生成$testname[$params...]形式的名称。

The documentation also talks about (suggests?) explicitly marking parametrized instances: https://pytest-dependency.readthedocs.io/en/latest/usage.html#parametrized-tests该文档还谈到(建议?)明确标记参数化实例: https : //pytest-dependency.readthedocs.io/en/latest/usage.html#parametrized-tests

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM