![](/img/trans.png)
[英]Pytest: Parameterize unit test using a fixture that uses another fixture as input
[英]How to parameterize a test depending on fixture parameters in pytest?
我有一个 Python 程序,它根据输入规范生成 C 代码。 我正在用 pytest 编写测试。 当然,测试策略包括对生成的 C 代码的一些测试。
对于这些测试,计划如下所示:
我们有一组目录,每个目录包含一个规范文件和一组适用的输入/预期 output 案例。
夹具将处理生成 C 代码并编译它。 该夹具将在一组规范文件(由测试脚本以编程方式读取)上进行参数化。 这样做的好处是,对于该规范下的所有测试用例,构建只能完成一次(因为构建成本很高)。
测试 function 将从夹具中获取GeneratedCode
object,使用特定输入运行它,并验证预期的 output。 这将在一组输入/输出案例(也由脚本以编程方式读取)上进行参数化。
这样,添加新的测试用例就像添加新的规范或测试用例文件一样简单。 无需在测试脚本中复制和粘贴代码。
我想象它看起来像这样:
# Get the list of specification files and test cases programmatically
specification_names = get_list_of_specifications()
test_cases = dict()
for spec in specification_names:
# get_list_of_test_cases() returns a list of (input, output) tuples
test_cases[spec] = get_list_of_test_cases(spec)
class GeneratedCode:
def __init__(spec):
"""Generate the C code for spec in a temp directory"""
self.name = spec
...
def build():
"""Build the generated C code"""
...
def run(input):
"""Run the code on given input."""
...
def cleanup():
...
@pytest.fixture(scope="module", params=specification_names)
def generated_code(request):
code = GeneratedCode(request.param)
code.build()
yield code
code.cleanup()
@pytest.mark.parametrize('test_input,expected_output', test_cases[???])
def test_generated_code(generated_code, test_input, expected_output):
assert generated_code.run(test_input) == expected_output
当然,这里的问题是@pytest.mark.parametrize()
不能每次都使用相同的测试用例集,因为它取决于生成代码的规范。 如果我们可以获得当前夹具的参数,我们可以在test_cases
字典中查找它,但我不确定如何做到这一点,或者是否有可能。
有没有办法做到这一点? 我还有其他方法可以处理这些测试吗?
通过将规范作为 generate_code 中元组的一部分传回,可能能够将数据连接在一起。
@pytest.fixture(scope="module", params=specification_names)
def generated_code(spec):
code = GeneratedCode(spec)
code.build()
yield code, spec
code.cleanup()
def test_generated_code(generated_code):
code, spec = generated_code
test_input, expected_output = test_cases[spec]
assert generated_code.run(test_input) == expected_output```
我能想到的另一种方法是使用subTest
,如果您可以访问unittest
,它是 python 标准库的一部分:
import unittest
class TestSequence(unittest.TestCase):
def _setup(self, spec):
self.code = GeneratedCode(spec)
self.code.build()
def tearDown(self):
self.code.cleanup()
def test_generated_code(self):
for spec, (test_input, expected_output) in test_cases.items():
with self.subTest(spec):
self._setup(spec)
assert self.code.run(test_input) == expected_output
@pytest.mark.parametrize
的indirect
参数可以帮助完成这项工作。 它基本上允许参数化测试 function 中的夹具。
specification_names = get_list_of_specifications()
test_cases = []
for spec in specification_names:
test_cases.extend([(spec, input, output) for (input, output) in
get_list_of_test_cases(spec)])
...
@pytest.fixture(scope="module")
def generated_code(request):
code = GeneratedCode(request.param)
code.build()
yield code
code.cleanup()
@pytest.mark.parametrize(
'generated_code,test_input,expected_output',
test_cases,
indirect=['generated_code'],
scope="module" # <-- This is important!
)
def test_generated_code(generated_code, test_input, expected_output):
assert generated_code.run(test_input) == expected_output
注意parametrize
装饰器中的scope="module"
。 如果未指定,它将默认为'function'
,并且在某些情况下(包括这个),这似乎优先于夹具指定的 scope。
对我来说,细节很模糊。 关于scope
甚至对@pytest.mark.parameterize
意味着什么的文档不是很清楚。 但是,似乎如果parametrize
中的所有参数都是indirect
的,则夹具使用自己的 scope,否则使用来自parametrize
的 scope。 但是,如果您有多个测试函数使用相同的夹具和indirect
,无论您指定什么,它们通常会在不同的范围内结束,我不知道为什么。 这是一个以前有问题的区域,现在可能仍然存在。
在任何情况下,上面的代码都应该做你想做的事情,但最好将夹具 scope 更多地视为性能优化,而不是依赖它来进行正确的测试行为(听起来你已经在做)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.