Why does Pytest perform a nested loop over fixture parameters(为什么Pytest在装置参数上执行嵌套循环)
问题描述
使用Pytest,我想编写一个测试函数,该函数接受多个fixture作为参数。每个灯具都有几个参数。
例如,在test_demo.py中,有一个函数test_squared_is_less_than_10,它将fixturenegative_integer和positive_integer作为参数,然后检查fixture参数的平方是否小于10。
import pytest
@pytest.fixture(params=[-1, -2, -3])
def negative_integer(request):
return request.param
@pytest.fixture(params=[1, 2, 3])
def positive_integer(request):
return request.param
def test_squared_is_less_than_10(negative_integer, positive_integer):
assert negative_integer ** 2 < 10
assert positive_integer ** 2 < 10
我预计当我在终端运行pytest命令时,总共应该执行6个测试,即positive_integer执行[-1, -2, -3],negative_integer执行[1, 2, 3]。
[(-1, 1), (-1, 2), (-1, 3), (-2, 1), (-2, 2), (-2, 3), (-3, 1), (-3, 2), (-3, 3)]。
以下是我运行pytest -v -k "test_squared_is_less"时的输出:
test_squared_is_less_than_10[-1-1] PASSED
test_squared_is_less_than_10[-1-2] PASSED
test_squared_is_less_than_10[-1-3] PASSED
test_squared_is_less_than_10[-2-1] PASSED
test_squared_is_less_than_10[-2-2] PASSED
test_squared_is_less_than_10[-2-3] PASSED
test_squared_is_less_than_10[-3-1] PASSED
test_squared_is_less_than_10[-3-2] PASSED
test_squared_is_less_than_10[-3-3] PASSED
这是不可取的,因为我只想执行6个测试,而不是9个。如果参数较多(比如20个),pytest将执行400个测试,而不是所需的40个测试,这是浪费计算时间。
如何处理此问题。
附注:我希望避免编写两个单独的测试,例如
test_negative_squared_is_less_than_10(negative_integer)和test_positive_squared_is_less_than_10(positive_integer)
推荐答案
您可以通过@pytest.mark.parametrize标记应用非笛卡尔参数化。您的代码,已重构:
import pytest
neg_params = [-1, -2, -3]
@pytest.fixture(params=neg_params)
def negative_integer(request):
return request.param
pos_params = [1, 2, 3]
@pytest.fixture(params=pos_params)
def positive_integer(request):
return request.param
@pytest.mark.parametrize(
"negative_integer, positive_integer",
zip(neg_params, pos_params),
indirect=True
)
def test_squared_is_less_than_10(negative_integer, positive_integer):
assert negative_integer ** 2 < 10
assert positive_integer ** 2 < 10
这篇关于为什么Pytest在装置参数上执行嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么Pytest在装置参数上执行嵌套循环
基础教程推荐
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
