gpt4 book ai didi

python - pytest-将 fixture 传递给mark.parametrize

转载 作者:太空宇宙 更新时间:2023-11-03 21:29:30 25 4
gpt4 key购买 nike

目的是对一组对象进行参数化,并且对于每个对象,对所有测试进一步进行参数化(从1到N)。我的对象可能非常大,这就是为什么我不想一次性加载所有对象的原因。测试的数量应等于所有对象的tests中的键的数量,而不是对象的数量。

目前,我正在尝试通过将固定装置传递到mark.parametrize来生成要运行的测试列表来实现此目的。但这在pytest.mark.parametrize中不起作用,它将some_fixture视为一个函数,并且出现以下错误:

AttributeError: 'function' object has no attribute 'list_of_tuples'


样例代码:

dict = {
'obj1': {
'tests': {'a1': 'b1', 'a2':'b2'}
},
'obj2': {
'tests': {'a3':'b3', 'a4':'b4', 'a5':'b5'}
}
}

class RandomClass():
def __init__(self, name, dict):
self._obj_name = name
self._dict = dict

self.list_of_tuples = None

def create_list_of_tuples(self):
self.list_of_tuples = []
for item in self._dict[self._obj_name]['tests']:
self.list_of_tuples.append((item, self._dict[self._obj_name]['tests'][item]))


@pytest.fixture(params=list(dict.keys())
def some_fixture(request, dict):
obj = RandomClass(name = request.param, dict=dict)
obj.create_list_of_tuples()
return obj

@pytest.mark.parametrize("var1, var2", some_fixture.list_of_tuples, indirect=True)
def test_something(var1, var2):

assert var1 == var2


非常感谢。

最佳答案

pytest不支持夹具作为参数化的一部分,夹具是在收集所有测试后制成的

关于python - pytest-将 fixture 传递给mark.parametrize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53612184/

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