01 / 20

Designing
Edge Case Tests

Learn to write tests that probe the boundaries and limits of your Python code: where bugs hide and programs break.

Python Software Testing

What Are Edge Cases?

An edge case is an input or scenario that sits at the extreme boundary of what a function is expected to handle. These are the values most likely to expose hidden bugs: empty collections, zero, negative numbers, maximum values, and unusual data types.

Understanding Boundary Conditions

A boundary condition is the exact point where behavior changes. For a function that accepts values in a range \([a, b]\), the boundaries are the values at, just inside, and just outside \(a\) and \(b\).

\(a\)
middle
\(b\)
\(a-1\)
\(b+1\)
\(a+1\)
\(b-1\)
Boundary Just inside Just outside

Why Edge Cases Matter

✕ Crashes
Unhandled edge cases cause runtime errors: IndexError, ZeroDivisionError, TypeError.
★ Silent Bugs
Code may return wrong answers without raising exceptions, creating subtle data corruption.
✓ Confidence
Thorough edge case tests give you confidence to refactor without introducing regressions.

The Edge Case Taxonomy

Edge Case Types Empty / Zero [], "", 0, None False, {}, set() Boundaries min / max off-by-one Special Values negative, float inf, NaN, huge Type Mismatches str vs int, mixed Duplicates / Order all same, reversed Size Extremes
1 element, \(10^6\)

The Boundary Value Rule

For any input with a valid range, always test these five values:

1
Minimum valid value. The smallest input the function should accept.
2
Just above minimum. One step inside the valid range: \(a + 1\).
3
A typical middle value. Confirms the "happy path" works.
4
Just below maximum. One step inside from the upper end: \(b - 1\).
5
Maximum valid value. The largest input the function should accept.

Edge Cases vs. Corner Cases

Edge Case Corner Case
Definition One parameter at its boundary Multiple parameters at boundaries simultaneously
Example Empty list input Empty list + zero divisor
Frequency Common, easier to identify Rare, harder to anticipate
Risk Level High: causes most bugs Very high: causes catastrophic failures
Analogy Edge of a square Corner where two edges meet

How to Design Edge Case Tests

1
Read the function signature. Identify every parameter and its expected type and range.
2
List the "zero" values. For each parameter: empty string, empty list, 0, None, False.
3
Identify boundaries. Find min/max valid values and test at, just inside, and just outside.
4
Consider special patterns. Duplicates, all-same elements, reversed order, single element.
5
Combine for corner cases. Cross two or more boundaries simultaneously to stress-test interactions.
6
Write assert statements. Each edge case gets its own test with a clear expected result.

Example 1: Testing find_max

def find_max(numbers): # Returns the largest number in a list result = numbers[0] for n in numbers: if n > result: result = n return result
✕ Empty list
[] raises IndexError. Should we handle it?
★ Single element
[42] should return 42.
★ All same
[5, 5, 5] should return 5.

Edge Case Tests for find_max

# Edge case tests for find_max def test_single_element(): assert find_max([42]) == 42 def test_all_same(): assert find_max([5, 5, 5]) == 5 def test_negatives(): assert find_max([-3, -1, -7]) == -1 def test_max_at_start(): assert find_max([99, 1, 2]) == 99 def test_max_at_end(): assert find_max([1, 2, 99]) == 99 def test_empty_list(): try: find_max([]) assert False # Should not reach here except IndexError: pass # Expected behavior

Example 2: Testing is_palindrome

def is_palindrome(text): # Returns True if text reads the same forwards and backwards cleaned = text.lower().replace(" ", "") return cleaned == cleaned[::-1]
✓ Empty string
"" is technically a palindrome.
★ Single char
"a" should return True.
★ Mixed case
"Racecar" should still be True.

Complete Tests for is_palindrome

# Edge case tests for is_palindrome assert is_palindrome("") == True # Empty string assert is_palindrome("a") == True # Single character assert is_palindrome("ab") == False # Two different chars assert is_palindrome("aa") == True # Two same chars assert is_palindrome("Racecar") == True # Mixed case assert is_palindrome("A man a plan a canal Panama") == True assert is_palindrome("hello") == False # Normal non-palindrome assert is_palindrome(" ") == True # Only spaces

Example 3: Testing safe_divide

def safe_divide(a, b): # Returns a / b, or None if b is zero if b == 0: return None return a / b
1
Division by zero: safe_divide(10, 0) should return None.
2
Zero divided by something: safe_divide(0, 5) should return \(0.0\).
3
Both zero: safe_divide(0, 0) is a corner case: should return None.
4
Very large numbers: safe_divide(\(10^{308}\), \(10^{-308}\)) tests float overflow.
5
Negative values: safe_divide(-10, 3) and safe_divide(10, -3) verify sign handling.

Common Mistakes

✕ Only Testing Happy Path
Writing tests only for typical inputs like [1, 2, 3] and never testing empty, single-element, or extreme inputs.
✕ Forgetting None / NaN
Not testing what happens when None, NaN, or unexpected types are passed as arguments to the function.
✕ Ignoring Off-by-One
Testing index 0 and index len-1 but forgetting to test index len (just past the end), which catches fencepost errors.
✕ No Negative Tests
Only testing that correct inputs give correct results. You must also test that invalid inputs raise the expected errors.
✕ Testing Implementation
Writing tests that depend on internal details (variable names, algorithm steps) instead of testing the function's observable behavior.

Edge Case Checklist

Input Type Edge Cases to Test Example
List / Array Empty, single element, all same, very large [], [1], [5,5,5]
String Empty, single char, spaces only, special chars "", "a", " "
Integer Zero, negative, very large, min/max 0, -1, \(2^{31}-1\)
Float Zero, negative, infinity, NaN, tiny 0.0, -0.0, float('inf')
Boolean Both True and False, truthy/falsy equivalents True, False, 0, ""
Dictionary Empty, missing keys, None values {}, {"k": None}

Question 1 of 5

Which of the following is the best edge case test for a function get_first(lst) that returns the first element of a list?

Question 2 of 5

A function clamp(x, low, high) should return x if \(low \leq x \leq high\), otherwise return the nearest bound. Which test set best covers boundary conditions?

Question 3 of 5

What is the difference between an edge case and a corner case?

Question 4 of 5

Which critical edge case is missing from these tests?

Question 5 of 5

You are testing a function binary_search(arr, target) that returns the index of target in a sorted list, or \(-1\) if not found. Which test case catches a common off-by-one error?