我正在开发一个计划用于多个项目的 django 可重用包。我使用 pytest 构建测试套件,我使用参数化帮助 pytest 运行具有多个配置的单个测试。
然而,我想使用不同的设置组合来运行我所有的测试
available_backends = [
'django_profile.auth_backends.drf.RestFramework',
'django_profile.auth_backends.kong.Kong',
]
def pytest_generate_tests(metafunc):
# if 'stringinput' in metafunc.fixturenames:
if 'auth_backend' in metafunc.fixturenames:
metafunc.parametrize(
'auth_backend',
available_backends
)
@pytest.fixture(params=['auth_backend', ])
def auth_backend(request, settings):
settings.DJANGO_PROFILE_AUTH_BACKEND = request.auth_backend
return settings
我尝试了上述方法,但这也意味着我必须向每个测试用例添加 auth_backend
,我认为这不是理想的。任何人都可以为我推荐一种使用不同设置组合运行所有测试的方法吗?
问候
最佳答案
请问你试过吗:
- 在 conftest.py 中使用 scope="session"的代码
- 直接在 params= 中使用 available_backends 列表而不是 pytest_generate_tests
https://docs.pytest.org/en/latest/fixture.html#parametrizing-fixtures
如果没有,你可以尝试使用这个:
@pytest.fixture(scope='session', params=[
'django_profile.auth_backends.drf.RestFramework',
'django_profile.auth_backends.kong.Kong',
])
def auth_backend(request, settings):
settings.DJANGO_PROFILE_AUTH_BACKEND = request.param
yield settings
关于python - 使用 Django 设置参数化测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47243507/