我正准备进入python的单元测试,但我很难找到解决以下问题的方法(source)。在
We have two functions, is_prime and print_next_prime. If we wanted to testprint_next_prime, we would need to be sure that is_prime is correct, asprint_next_prime makes use of it. In this case, the function print_next_prime is one unit, and is_prime is another. Since unit tests test only a single unit at a time, we would need to think carefully about how we could accurately test print_next_prime.
def is_prime(number):
"""Return True if *number* is prime."""
for element in range(number):
if number % element == 0:
return False
return True
def print_next_prime(number):
"""Print the closest prime number larger than *number*."""
index = number
while True:
index += 1
if is_prime(index):
print(index)
如何为这两种方法编写单元测试?不幸的是,消息来源从来没有给出这个问题的答案。在
代码已在blog的后面部分修复,首先必须定义
is_prime
这是一个案例
^{pr2}$test_is_five_prime
的单元测试用例。还有其他例子,如test_is_four_non_prime
,test_is_zero_not_prime
。同样,您可以为其他函数print_next_prime
编写测试。你可以用同样的方法。在相关问题 更多 >
编程相关推荐