Content · Programming · Python

Python Test Happy Paths, Edge Cases, and Invalid Inputs

Test normal values, boundaries and invalid input as separate cases.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Test normal values, boundaries and invalid input as separate cases.

What It Does and Why It Matters


Happy-path tests verify expected ordinary behavior. Edge cases exercise empty collections, zero, exact boundaries and unusual but valid values. Invalid-input tests verify clear failures rather than silent corruption. Choose cases based on the function contract, not merely on convenient example values.

For a real test suite, use unittest.assertRaises or pytest.raises instead of manually handling every error.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

def divide_total(total: float, people: int) -> float:
    if people <= 0:
        raise ValueError("people must be positive")
    return total / people
assert divide_total(30, 3) == 10
assert divide_total(0, 1) == 0
try:
    divide_total(30, 0)
except ValueError:
    print("Invalid input rejected")

How the Code Works


  • The definition of divide_total creates reusable behavior; its body runs only when called or instantiated as appropriate.
  • The structured block controls error handling or resource lifetime, including what happens when an operation fails.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.

Expected output:

Invalid input rejected

For the demonstrated inputs, Python displays 'Invalid input rejected'. This result follows from the operation described above; change one input at a time to understand which rule matters.

What to Notice


Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.

If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.

Where You Would Use This


Start with an independently checkable requirement and test both normal and boundary inputs. A passing assertion documents only the cases that were exercised. Keep tests deterministic so a failure identifies a real change rather than environmental noise.

Practice Exercise


Add tests for negative people and a non-divisible bill total.

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Treating a function as adequately tested after one successful input.
  • Overlooking the documented condition: invalid-input tests verify clear failures rather than silent corruption.

Key Takeaways


  • Test normal values, boundaries and invalid input as separate cases.
  • Happy-path tests verify expected ordinary behavior.
  • Add tests for negative people and a non-divisible bill total.
Share this resourceSend this page to someone who may find it useful.