【发布时间】:2016-06-22 15:37:27
【问题描述】:
我的问题是 - 是否可以使用来自夹具的返回值作为参数化的值? 问题是 - 我想动态获取参数化的可能值(例如,虚拟服务器上的可用系统)。当其中一个装置创建虚拟服务器时,我可以访问这些。测试看起来像这样(伪代码):
[conftest.py]
@pytest_fixture(scope='session')
def test_server(request):
test_server = Server([default_params])
test_server.add()
def fin():
test_server.delete()
request_addfinalizer(fin)
return test_server()
[tests.py]
def test_basic_server(test_server):
systems = test.server.get_available_systems()
for system in systems:
test_server.install(system)
test_server.run_checks()
test_server.uninstall(system)
def test_other(test_server):
[other tests]
etc
这样,为每个会话添加一个服务器,然后在其上运行所有测试,会话结束后,服务器被删除。但是有没有办法使用会话开始时添加的服务器方法来获取@pytest.mark.parametrize 中的可用系统而不显式列出它们(静态地作为参数化中的列表)?这样每个系统都会在单独的测试中运行。
我尝试在另一个夹具中使用 test_server 然后返回列表(与 test_server 夹具返回 test_server 的方式相同,但我不能将其用作参数化中的值 - 因为在任何测试中调用 test_server 夹具之前评估装饰器,获取列表取决于 test_server 夹具。
这将是理想的:
[tests.py]
@pytest.mark.parametrize('system',[systems_list <- dynamically generated
when the server is created])
def test_basic_server(test_server,system):
test_server.install(system)
test_server.run_checks()
test_server.uninstall(system)
这只是一个非常基本的示例,在我的测试中,我需要根据多个场景和值进行参数化,当我静态执行时,我最终会得到巨大的数组。
但原理保持不变 - 基本上:我可以在使用此夹具的第一次测试运行之前调用夹具,或者 pytest.mark.parametrize() 如何访问夹具值?
【问题讨论】: