Testing Simple Cases

Concepts covered: Testing

Before testing complex inputs, always verify your code works with simple cases. If it fails on easy inputs, it will definitely fail on hard ones.

Start simple

When testing a function that processes lists, try these in order:

Example: Testing a function that finds the maximum:

def find_max(numbers):
    if not numbers:
        return None
    largest = numbers[0]
    for num in numbers:
        if num > largest:
            largest = num
    return largest

# Test cases
print(find_max([]))
print(find_max([5]))
print(find_max([3, 7]))
print(find_max([4, 2, 9, 1, 7]))
print(find_max([-3, -7, -1, -5]))

Output:

None
5
7
9
-1

Edge cases

Edge cases are inputs at the boundaries of what's valid. They're where bugs most commonly hide.

Handling edge cases is what separates working code from production-ready code.

Unchecked assumptions about inputs have destroyed at least one spacecraft.

Testing empty, single-element and 2-element inputs first catches most bugs before you reach complex inputs, and you will spend far less time debugging later.

Most of the distance between "working code" and "production-ready code" is edge cases.


Decomposing a problem into clear, logical steps is the most important skill in programming, and the Python Builder has hands-on challenges to practice 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.