[英]Pytest: associate parameterized fixtures with expected results
Say I have the following test: 说我有以下测试:
@pytest.fixture(params=['a'])
def ascii(request):
return ord(request.param)
def test_ascii(ascii):
assert ascii == 97
This works great. 这很好。 Now say I'd like to add
'b'
as a parameter. 现在说我想添加
'b'
作为参数。 Ideally I could just decorate the test with something like @pytest.mark.parametrize('ascii_val', [97, 98])
, add ascii_val
as an agrument to the test and assert ascii == ascii_val
. 理想情况下,我可以使用
@pytest.mark.parametrize('ascii_val', [97, 98])
类的东西来装饰测试,将ascii_val
添加到测试中并断言ascii == ascii_val
。 Pytest, however, would also assert 'a'
against 98 and 'b'
against 97. 但是Pytest也会针对98断言
'a'
,针对97断言'b'
。
Is there any way for me to associate 'a'
with 97 and 'b'
with 98? 我有什么办法将
'a'
与97关联,将'b'
与98关联? I'm asking because I'm going to have a lot of tests like test_ascii
where I'll be checking that some input consistently outputs the same output given different analysis techniques. 我问是因为我要进行很多测试,例如
test_ascii
,在这些测试中,我将检查某些输入在给定不同分析技术的情况下始终输出相同的输出。
At least for the simple example you're giving, why use a parametrized fixture instead of parametrizing your test? 至少对于您给出的简单示例,为什么要使用参数化夹具而不是对测试进行参数化?
Something like this should work: 这样的事情应该起作用:
@pytest.mark.parametrize('char, expected', [('a', 97), ('b', 98)])
def test_ascii(char, expected):
assert ord(char) == expected
If you really wanted to use a fixture, you could always return a (char, expected)
tuple from it and work with that. 如果您确实想使用固定装置,则总是可以从中返回一个
(char, expected)
元组并使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.