Chapter 8: Testing Times

Mull It Over

  1. Write a test harness for the greatest_common_divisor code example earlier in this chapter. Make it as exhaustive as you can. How many individual test cases have you included?

    1. How many of these passed?

    2. How many failed?

    3. Using these tests, identify any faults and repair the code.

There are a large number of tests you should run, even though there are very few invalid input combinations. Thinking of invalid inputs first: Test for zero. It may or may not be an invalid value (we’ve seen no spec, so we can’t tell), but you’d expect the code to cope reasonably with it.

Next, write tests considering combinations of usual inputs (say of 1, 10, and 100 in all orders). Then try numbers with no common multiple, like 733 and ...

Get Code Craft now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.