Skip to content

What makes a good test

Read · 1 of 3

Expected first, then run

A test is an input plus the answer you expect, worked out before you run the code. If you run first and then decide the output "looks right", you'll believe whatever the code says.

Good tests are chosen on purpose: one normal case, then each edge from the checklist, then the biggest input allowed. Five well-chosen tests beat a hundred random ones.

Read this lesson as text

An input, the answer you expect, and a reason for choosing it.

Expected first, then run

A test is an input plus the answer you expect, worked out before you run the code. If you run first and then decide the output "looks right", you'll believe whatever the code says.

Good tests are chosen on purpose: one normal case, then each edge from the checklist, then the biggest input allowed. Five well-chosen tests beat a hundred random ones.

Pick the test set

def second_biggest(nums):
    # the second biggest different value, or -1
    ...

You're testing second_biggest. Which set of tests would catch the most bugs?

  • 1 2 3, 4 5 6 and 7 8 9
  • 1 2 3, 5 5 5, 7, -1 -2 and 3 3 1
  • One test with 1,000 random numbers
Show the answer

1 2 3, 5 5 5, 7, -1 -2 and 3 3 1. The second set covers normal, all-equal, a single number, negatives and repeats. The first set tests the same easy case three times, and a random list almost never contains the tricky patterns.

Break it: Break the word counter

This should print how many words are in a line. Words are separated by one or more spaces.

s = input()
print(len(s.split(" ")))

Input rules: one line of up to 100 characters: letters and spaces, with at least one word.