import math
def getCircumference():
radius = int(input("Please enter a radius size: "))
circumference = 2 * math.pi * radius
print("The circumference of the circle is", circumference)
def test_get_circumference():
# using pytest.approx to avoid problems related to floating point precision
assert get_circumference(7) == pytest.approx(43.982297150257104)
assert get_circumference(3.678) == pytest.approx(23.109555559806516)
单元测试的目的是测试函数的有效功能。这最好通过检查函数是否为已知输入返回正确的结果来实现。您的函数不接受任何输入,也不返回任何结果,因此测试要比应该的复杂得多。它在其他情况下也不太有用
我对
getCircumference
函数的期望是:因此:
然后,测试将执行以下操作:
另一方面,如果您真的想按原样测试函数,您应该创建一个更复杂的测试,它将覆盖
sys.stdin
(为了伪造用户输入)和sys.stdout
(为了验证输出)相关问题 更多 >
编程相关推荐