I'm tying to get into unit testing for python and I'm having trouble with finding a way to tackel the following problem (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)
How would you write a unit test for both of these methods? Unfortunately the source never gives an answer to this question.
is_prime
function's broken as it'll attempt to divide by zero.