[英]Testing of Conditional Statement using 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
應該為您解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.