python - 使用 Pytest 测试条件语句
问题描述
当我运行测试时,它显示只有 1 个测试通过。如何使用 test_function 使其显示所有测试均已通过。
请注意, eval() 函数不带任何参数。
import pytest
def eval():
a=1 #got this value after calling some function (this can be 1,2,3 or any value)
if a ==2:
return 8
elif a == 3:
return 4
else:
return 42
@pytest.mark.parametrize("expected", [
(8),
(4),
(42),
])
def test_eval(expected):
assert eval() == expected
解决方案
好的,在评论中澄清之后,这a
是一个全球性的......如果不是这样会更好。:)
但如果你不能改变它的签名,
import pytest
def eval():
if a == 2:
return 8
elif a == 3:
return 4
else:
return 42
@pytest.mark.parametrize(
"input_value, expected", [(2, 8), (3, 4), (4, 42)]
)
def test_eval(input_value, expected):
global a
a = input_value
assert eval() == expected
应该为您解决问题。
推荐阅读
- ios - 您如何更改 Google Maps 的集群图标?
- python - Delete in Tkinter Python
- java - Talend Studio 中的 java.lang.NoClassDefFoundError
- php - PHP将获取变量添加到URL
- python - 无法从 utils 导入 process_tweets
- mysql - Ubuntu 20.04 - 停止 MySQL。现在它不会启动(错误 13,权限被拒绝)
- javascript - 在另一个页面上显示整个多个 div
- php - 如何在 wordpress woocommerce 中创建智能捆绑产品?
- c++ - 从源代码编译虚幻引擎后缺少 dxgi.lib
- c++ - 如何拆分字符串并将每个数字存储在整数向量中?