我有一些固定装置在conftest.py实际测试功能中运行良好。但是,我想pytest_generate_tests()根据其中一些夹具中的数据参数化一些测试。
我想做什么(简化):
-- conftest.py --
# my fixture returns a list of device names.
@pytest.fixture(scope="module")
def device_list(something):
return ['dev1', 'dev2', 'dev3', 'test']
-- test001.py --
# generate tests using the device_list fixture I defined above.
def pytest_generate_tests(metafunc):
metafunc.parametrize('devices', itertools.chain(device_list), ids=repr)
# A test that is parametrized by the above function.
def test_do_stuff(devices):
assert "dev" in devices
# Output should/would be:
dev1: pass
dev2: pass
dev3: pass
test: FAIL
当然,我遇到的问题是在 pytest_generate_tests() 中,它抱怨 device_list 未定义。如果我尝试将其传递给 pytest_generate_tests(metafunc, device_list),则会出现错误。
E pluggy.callers.HookCallError: hook call must provide argument 'device_list'
我想这样做的原因是我在不同文件中的一堆不同测试中使用了“device_list”列表,我想使用 pytest_generate_tests() 使用相同的列表对测试进行参数化。
这是不可能的吗?如果我必须在该函数中复制我的固定装置,那么使用 pytest_generate_tests() 有什么意义?