gpt4 book ai didi

parameterization - pytest 测试需要在收集阶段和设置时进行参数化

转载 作者:行者123 更新时间:2023-12-04 16:44:59 28 4
gpt4 key购买 nike

我有一些测试,我想使用一些需要在收集阶段进行参数化的参数和一些需要在设置时进行参数化的参数进行参数化。我无法使用metafunc.parametrize 在 pytest_generate_test Hook 中,因为我需要一些固定装置具有 indirect=True 以将 argname 作为 request.param 传递,但其他参数需要具有 indirect=False。

有什么办法吗?

这是我的测试是什么样子以及我想做什么的示例:

def pytest_generate_tests(metafunc):
if metafunc.function.__name__ == 'test_example':
argnames = []
argvalues = []
parameters = getattr(metafunc.function, 'paramlist', ())
for p in parameters:
if type(p) == list:
argnames = tuple(['myfixture'] + p)
else:
argvalues.append = tuple(['std'] + p['argvalues'])
argvalues.append = tuple(['pro'] + p['argvalues'])
# I want to do the following, but it won't work since some of the
# args need indirect set to true and some need indirect set to false.
metafunc.parametrize(argnames, argvalues, indirect=True)
elif 'myfixture' in metafunc.fixturenames:
# we have existing tests which use the fixture, but only with
standard
metafunc.parametrize("myfixture", "std")
else:
# we have existing tests which use older style parametrization,
non-fixture
for p in getattr(metafunc.function, 'paramlist', ()):
metafunc.addcall(funcargs=p)


def params(decolist):
def wrapper(function):
function.paramlist = decolist
return function
return wrapper

@pytest.fixture
def myfixture(request):
If request.param == 'std':
myfix = SomeObject()
elif request.param == 'pro':
myfix = SomeOtherObject()
def fin():
myfix.close()
request.addfinalizer(fin)
return myfix

@params([
['color', 'type'],
{ 'argvalues': [ 'blue', 'cat'] },
{ 'argvalues': ['pink', 'dog'] }
])
def test_example(myfixture, color, type):
# this is the new test we want to add

def test_something(myfixture):
# existing test which only uses std fixture

@params([
{'arg1': 1, 'arg2': 2},
{'arg1': 3, 'arg2': 5}
])
def test_old_style(arg1, arg2):
# existing tests which don't use fixtures

感谢您阅读本文!我知道它很长。

最佳答案

根据设计,所有参数化都发生在收集时

关于parameterization - pytest 测试需要在收集阶段和设置时进行参数化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15775118/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com