来自pytest documentation:
@ pytest.mark.parametrize允许在测试函数或类中定义多组参数和固定装置。
这似乎意味着pytest.mark.parametrize
可以标记一个测试以使用多套夹具运行?我可以找到很多参数化参数的示例,但是我不知道如何参数化不同的夹具。
我认为this answer接近,但这实际上只是参数化参数,然后解决测试主体中的不同问题。
是否可以使用不同的固定装置将测试标记为可以多次运行?
请注意,我正在尝试执行以下操作:
import pytest
# some data fixutres
@pytest.fixture()
def data1():
"""Create some data"""
@pytest.fixture()
def data2():
"""Create some different data"""
@pytest.fixture()
def data3():
"""Create yet different data"""
# The tests
@pytest.mark.parametrize('data', [data1, data2])
def test_foo(data):
"""Test something that makes sense with datasets 1 and 2"""
@pytest.mark.parametrize('data', [data2, data3])
def test_bar(data):
"""Test something that makes sense with datasets 2 and 3"""
答案 0 :(得分:1)
您可以使用pytest-lazy-fixture插件执行此操作:
import pytest
from pytest_lazyfixture import lazy_fixture
@pytest.fixture()
def fixture1():
return "data1"
@pytest.fixture()
def fixture2():
return "data2"
@pytest.fixture()
def fixture3():
return "data3"
@pytest.mark.parametrize("data", [lazy_fixture("fixture1"),
lazy_fixture("fixture2")])
def test_foo(data):
assert data in ("data1", "data2")
@pytest.mark.parametrize("data", [lazy_fixture("fixture2"),
lazy_fixture("fixture3")])
def test_bar(data):
assert data in ("data2", "data3")
请注意,有人建议直接向pytest添加类似功能:https://docs.pytest.org/en/latest/proposals/parametrize_with_fixtures.html