小编cyb*_*unk的帖子

如何参数化测试,从参数化夹具中获取参数?

我提前道歉重复1000次单词参数.我的用例如下.

我正在使用pytest来测试解析器,该解析器从在线商店中解析产品页面中的字段.

我已经参数化了一个夹具,因此每个夹具都会导入一个产品的数据.根据数据,我的意思是HTML源代码和带有预期值的字段列表.在线下我有一个参数化测试,它接受一个元组列表(字段,期望值),以便每个字段都有自己的测试.

基本上,"裸骨"问题会像这样:

from pytest import fixture, mark

products = [
    {
    'text': 'bla bla', 
    'fields': [('bla', 0), ('foo', -1)]
    },
    {
    'text': 'foo bar', 
    'fields': [('bla', -1), ('foo', 0), ('bar', 4)]
    }
]

@fixture(params=products)
def product(request):
    return request.param


@mark.parametrize('field_key, field_value', product['fields'])
def test_parser(product, field_key, field_value):
    assert product['text'].find(field_key) == field_value
Run Code Online (Sandbox Code Playgroud)

@mark.parametrize装饰器的上下文中,product不被解释为fixture,因此pytest返回:

TypeError: 'function' object has no attribute '__getitem__'
Run Code Online (Sandbox Code Playgroud)

pytest正在进行大量的内省魔术,我无法找到解决方案.我看了一下这个问题,但这不是我想要的.有没有办法实现这个目标?谢谢.

python parameters fixtures pytest python-2.7

6
推荐指数
1
解决办法
428
查看次数

标签 统计

fixtures ×1

parameters ×1

pytest ×1

python ×1

python-2.7 ×1