[英]How to parametrize a test with several pytest markers?
Say that I want to run the same test code with several pytest markers.假设我想用几个 pytest 标记运行相同的测试代码。 One solution is to copy-paste the test with the markers applied:
一种解决方案是使用应用的标记复制粘贴测试:
@pytest.mark.A
def test_a():
# same
# code
...
@pytest.mark.B
def test_b():
# same
# code
...
One "hack" I found is to parametrize a dummy parameter and apply different markers using pytest.param
:我发现的一个“hack”是参数化一个虚拟参数并使用
pytest.param
应用不同的标记:
@pytest.mark.parametrize("_", [
pytest.param(None, marks=pytest.mark.A),
pytest.param(None, marks=pytest.mark.B),
])
def test(_):
# same
# code
...
But it is clumsy and adds unused parameters.但它很笨拙并添加了未使用的参数。 Is there a simpler way?
有没有更简单的方法?
You can create a function that yields pytest.param
and use a fake name (an empty string) in parametrize
to avoid sending a parameter to the test您可以创建产生 pytest.param 的
pytest.param
并在参数化中使用假名称(空字符串)以避免将parametrize
发送到测试
def add_markers():
for mark in [pytest.mark.A, pytest.mark.B]:
yield pytest.param(marks=mark)
@pytest.mark.parametrize('', add_markers())
def test_example():
# code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.