有一个正在讨论的 API 扩展,它允许在设置资源中使用 funcargs,您的用例就是一个很好的例子。请参阅此处查看正在讨论的 V2 草案:http://pytest.org/latest/resources.html http://pytest.org/latest/resources.html
今天,您可以这样解决您的问题::
# contest of conftest.py
import string
def pytest_addoption(parser):
parser.addoption("--backend" , default="test_backend",
help="run testx for the given backend, default: test_backend")
def pytest_generate_tests(metafunc):
if 'backend' in metafunc.funcargnames:
if metafunc.config.option.backend:
backend = metafunc.config.option.backend
backend = backend.split(',')
backend = map(lambda x: string.lower(x), backend)
metafunc.parametrize("backend", backend, indirect=True)
def setupmodule(backend):
print "copying for", backend
def pytest_funcarg__backend(request):
request.cached_setup(setup=lambda: setupmodule(request.param),
extrakey=request.param)
return request.param
给定一个包含两个测试的测试模块:
def test_me(backend):
print backend
def test_me2(backend):
print backend
然后你可以运行来检查事情是否按照你的预期发生:
$ py.test -q -s --backend=x,y
收集了 4 件物品
复制 x
X
.复制 y
y
。X
.y
0.02秒内4人通过
由于有两个正在测试的后端,您将获得四个测试,但模块设置仅针对模块中使用的每个后端完成一次。