F. *_*iot 5 python pytest pytest-dependency
我有一个函数和一个测试
def foo(a):
return bar(a)
@pytest.mark.parametrize(
'number',
[1,2,3]
)
@pytest.mark.dependency
def test_foo(number):
assert foo(number) > SOME_CONST # Simplistic example, real case is more nuanced
Run Code Online (Sandbox Code Playgroud)
我正在使用 pytest 和 pytest_dependency 模块。foo是许多其他测试中使用的函数。我有一个函数,我想依赖它test_foo,下面的代码不起作用:
@pytest.mark.dependency(depends=['test_foo'])
@pytest.mark.parametrize(
'param',
itertools.permutations(['a','b','c','d','e'],2),
ids=repr,
)
def test_bar(param):
...
important_result = foo(param)
...
Run Code Online (Sandbox Code Playgroud)
理论上来说,如果test_foo失败,那么test_bar就会被跳过。但是,当我参数化 时,无论 的结果如何,都会跳过 的test_bar每个实例化。test_bartest_foo
澄清一下,此代码按预期工作(未跳过 test_bar):
@pytest.mark.dependency(depends=['test_foo'])
def test_bar():
param = some_fnc(['a', 'b'])
...
important_result = foo(param)
...
Run Code Online (Sandbox Code Playgroud)
您的问题与pytestnode_id分配给每个测试有关(请参阅pytest-dependency#names)。
根据您的问题,我理解您想要test_bar依赖 的所有参数化test_foo,因此在这种情况下您需要执行以下操作:
@pytest.mark.dependency(depends=['foo[1]', 'foo[2]', 'foo[3]'])
@pytest.mark.parametrize(
'param',
itertools.permutations(['a', 'b', 'c', 'd', 'e'], 2),
ids=repr,
)
def test_bar(param):
...
Run Code Online (Sandbox Code Playgroud)
但由于参数化可能比数字更复杂,因此建议实施以下方法:
@pytest.mark.parametrize(
'number',
[
pytest.param(1, marks=pytest.mark.dependency(name='foo1')),
pytest.param(2, marks=pytest.mark.dependency(name='foo2')),
pytest.param(3, marks=pytest.mark.dependency(name='foo3'))
]
)
def test_foo(number):
...
@pytest.mark.dependency(depends=['foo1', 'foo2', 'foo3'])
@pytest.mark.parametrize(
'param',
itertools.permutations(['a', 'b', 'c', 'd', 'e'], 2),
ids=repr,
)
def test_bar(param):
...
Run Code Online (Sandbox Code Playgroud)
如果您想要每个参数化都有依赖关系,则必须将每个param值的依赖关系显式设置test_bar到其相应的foo?测试中。
| 归档时间: |
|
| 查看次数: |
1298 次 |
| 最近记录: |