Testing Simple Cases

Concepts covered: Testing

A function that has been written hasn't been checked. The check is running it on inputs small enough that you know the answer without the function: an empty list, 1 item, 2 items, values at the boundary, values that are all the same. Each such run costs a few seconds, and each one that disagrees with your known answer is a bug found before a real dataset hides it among 10 million rows.

A function that looks right

This function is meant to return the largest value in a list of temperature changes, which can be negative. It reads correctly at a glance.

def largest(values: list) -> int:
    best = 0
    for value in values:
        if value > best:
            best = value
    return best

Pick small inputs with answers you know before running anything, and write the expected answer beside each:

InputExpectedWhy
[4]4One value, so it is the largest.
[3, 9, 2]9Ordinary case.
[-3, -7]-3Both negative; -3 is closer to zero.
[]no answer yetNothing to be largest; the function needs a decision.
def largest(values: list) -> int:
    best = 0
    for value in values:
        if value > best:
            best = value
    return best

print(largest([4]), "expected 4")
print(largest([3, 9, 2]), "expected 9")
print(largest([-3, -7]), "expected -3")

Output:

4 expected 4
9 expected 9
0 expected -3

Of the 3 rows, 2 agree and the third does not. With best starting at 0, no negative value ever beats it, so the function returns a 0 that was never in the list. The fix is the start value from the best so far pattern: best = values[0]. The empty list row is the other finding. values[0] fails on an empty list, so the function must decide what empty means. Here the caller agreed that an empty list returns 0. A temperature change of 0 is a real value, though, so a result of 0 can no longer say whether the list was empty. Choosing a different value to mean 'nothing' is a decision for a later lesson.

def largest(values: list) -> int:
    if len(values) == 0:
        return 0
    best = values[0]
    for value in values:
        if value > best:
            best = value
    return best

print(largest([-3, -7]), "expected -3")
print(largest([]), "expected 0")
print(largest([5, 5, 5]), "expected 5")

Output:

-3 expected -3
0 expected 0
5 expected 5

The same style of printing now agrees on every row. The [5, 5, 5] case was added because a list of equal values is where a comparison written as >= instead of > shows a difference in which item wins. Here neither choice changes the printed answer.

Choosing the small cases

Each case targets a specific kind of mistake, which is why 6 small runs find more than 1 large one. A list of 10000 real orders contains all of these cases somewhere, but when the total is off by 3 nobody can say which row caused it. With 2 items, the whole trace fits in your head.

Expected first, run second

The order matters. Writing the expected answer before running the code means the code can't talk you into its answer. Running first and then nodding at the output is how a 0 for [-3, -7] gets accepted, because 0 looks like a plausible number until you remember that it was never in the list.

Do
  • Write the expected answer next to each input before running.
  • Start with the empty list and 1 item.
  • Put the boundary value in a test on its own.
Avoid
  • Test only with the example from the problem statement.
  • Run first and decide afterwards whether the output looks right.
  • Skip negatives because the real data is 'always positive'.

Practice

Which small input finds the problem in this function?

def average(values: list) -> float:
    total = 0
    for value in values:
        total = total + value
    return total / len(values)

Capstone: one problem, all 5 habits

A logistics team asks: given a list of parcel weights in kilograms, how many parcels are over the 20 kilogram limit? Walk it through every section of this lesson before the drill below hands you a version with a mistake in it:

About this lesson

This section is part of the Problem Solving: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.