我可以想到两种方法来做到这一点。一种使用参数化的夹具,另一种参数化测试功能。由您决定哪个人更优雅。
这是参数化的测试函数:
import itertoolsimport pytestnumbers = [1,2,3,4,5]vowels = ['a','e','i','o','u']consonants = ['x','y','z']@pytest.mark.parametrize('number,vowel,consonant', itertools.product(numbers, vowels, consonants))def test(number, vowel, consonant): pass
值得注意的是,参数装饰器的第二个参数可以是可迭代的,而不仅仅是列表。
通过参数化每个灯具的方法如下:
import pytestnumbers = [1,2,3,4,5]vowels = ['a','e','i','o','u']consonants = ['x','y','z']@pytest.fixture(params=numbers)def number(request): return [email protected](params=vowels)def vowel(request): return [email protected](params=consonants)def consonant(request): return request.paramdef test(number, vowel, consonant): pass
你的直觉是正确的。通过参数化多个灯具中的每个灯具,pytest负责创建所有出现的排列。
测试输出是相同的。这是一个示例(我使用-vv选项运行py.test):
test_bar.py:22: test[1-a-x] PASSEDtest_bar.py:22: test[1-a-y] PASSEDtest_bar.py:22: test[1-a-z] PASSEDtest_bar.py:22: test[1-e-x] PASSEDtest_bar.py:22: test[1-e-y] PASSEDtest_bar.py:22: test[1-e-z] PASSEDtest_bar.py:22: test[1-i-x] PASSED
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)