Skip to content

Testing with assert and test cases

Write assert test cases for ordinary, edge and error inputs, and find the bugs that only an edge case reveals.

It opens on

average(xs) returns sum(xs) / len(xs), and it passes tests on [2, 4, 6] and on [5]. Does it work for every list?

Why it matters

Code that passes a few happy examples often fails at a boundary or on empty input; writing tests for ordinary, edge and error cases is how programmers catch that before users do, and choosing such cases is a common exam and interview question.

Good to know first

  • Booleans and comparisons: == and > give True or False, and 90 > 90 is False
  • if, elif and else: the first true branch runs
  • Defining functions: a function returns a value that a test can check

Step by step

  1. assert and ordinary cases

    Write assert statements that check a function's results on ordinary inputs, and read what happens when one fails.

    Here the run printed ordinary tests passed, so average([2, 4, 6]) gave 4 and average([5]) gave 5.

    A common mix-up

    If a function passes its tests it is correct.

    In fact: It is correct only on those inputs; tests that all look alike, like [2, 4, 6] and [5], can miss whole classes of bug.

  2. Edge cases at the boundary

    Choose boundary test cases for a function with ranges, and use a failing test to find and fix the bug.

    Tests at 95, 85, 72 and 40 all passed, and the run printed ordinary tests passed.

    A common mix-up

    Testing values from the middle of each range is thorough.

    In fact: Mistakes in conditions show only at the edges, where > and >= differ, so test on and next to each boundary.

  3. Error cases and floats

    Make a function raise a clear error for bad input, test that it does, and test float results with a tolerance instead of ==.

    The run printed empty list raises ValueError, so the error case passes.

    A common mix-up

    A test that expects an error is just a test that crashes.

    In fact: It passes when the expected error is raised and fails if the function returns normally.

Try it

Write two ordinary tests for a function double(x) that returns 2 * x.Answer

For example assert double(3) == 6 and assert double(-4) == -8

Hint: Include a negative number

A function is_teen(age) returns 13 < age < 19. Which boundary tests show it is wrong for the rule 13 to 19 inclusive?Answer

is_teen(13) and is_teen(19) both give False but should be True

Hint: Test the ends exactly

Write a test that average([1, 2, 4]) is about 2.333.Answer

r = average([1, 2, 4]) then assert abs(r - 7 / 3) < 1e-9

Hint: 7 / 3 is the exact value

If someone changed average to return sum(xs) // len(xs), would these two tests notice?Answer

No; 12 // 3 is 4 and 5 // 1 is 5, the same numbers, so both still pass; a test like [1, 2] expecting 1.5 would catch it.

Why does letter(90) not return A?Answer

90 > 90 is False, so the first branch is skipped; 90 > 80 is True, so it returns B; the condition should be score >= 90.

Testing with assert and test cases

30 minutes every month. No card.