Module 19 · Debugging, Testing and Best Practices — topic 9 of 15
Writing test cases
Choosing what to test: normal cases, edge cases and error cases, equivalence classes and boundary values, the Arrange-Act-Assert structure, table-driven tests, and randomised checks against a trusted reference.
Learning objectives
- Choose test cases using equivalence classes and boundaries
- Structure tests with Arrange-Act-Assert
- Write table-driven and randomised tests
Definition
A test case is a specific input together with the expected outcome. Good test cases are chosen systematically so that a small number of tests covers the behaviours most likely to contain bugs.
Introduction
Testing every possible input is impossible. Testing the right inputs — the boundaries, the empty cases, the invalid ones — catches most bugs with a handful of tests.
Why it matters
Thoughtful test cases find bugs that random poking misses and keep test suites short enough to maintain.
Detailed explanation
Test normal cases (typical inputs), edge cases (empty, single item, zero, maximum) and error cases (invalid input that should be rejected).
Equivalence classes group inputs that should behave the same way; test one representative from each class (for marks: below 50, 50–74, 75–89, 90–100, invalid).
Bugs cluster at boundaries, so test values on and next to each boundary: 49 and 50, 74 and 75, 0 and 100, and just outside the valid range.
Arrange-Act-Assert gives each test a clear shape: set up the data, perform one action, check the result.
Table-driven tests list (input, expected) pairs and check them in a loop or with pytest.mark.parametrize.
Randomised tests generate many inputs and compare against a trusted reference (such as sorted()) or check properties that must always hold.
Each test should check one behaviour and have a name describing it, such as test_rejects_marks_above_100.
When a bug is found, first write a test that reproduces it, then fix the code.
Syntax
- Normal, edge and error cases.
- Test on and around boundaries.
- Arrange, act, assert.
Code examples
Boundary values
8 cases, 0 failures -1 rejected 101 rejected
Arrange, act, assert
test_transfer_moves_money passed
More examples
Randomised tests against a reference
200 random cases passed True
Step-by-step explanation
- Python's sorted() is the trusted reference, so any difference reveals a bug in my_sort.
- A fixed seed makes the 'random' cases repeatable when a failure needs investigating.
- Hand-picked edge cases complement random ones: empty, single, reversed, duplicates and negatives.
Expected output
Running “Randomised tests against a reference” prints exactly:
Practical applications
- Banking software tested at limits such as zero balances and maximum transfers.
- Property-based testing libraries such as Hypothesis.
Best practices
- Cover normal, edge and error cases.
- Test boundaries on both sides.
- Name tests after the behaviour they check.
Common mistakes
Only testing the happy path
Avoid
Prefer
Bugs hide at boundaries and in invalid input.
Important notes
- Code coverage tools show which lines your tests never run, hinting at missing cases.
Key takeaways
- Choose cases with equivalence classes and boundaries.
- Structure tests as arrange-act-assert.
- Use tables and randomised reference checks.
Try it yourself
- 1Design test cases for a function that validates Indian PIN codes (6 digits, not starting with 0).
- 2Write a randomised test comparing your own max function with Python's max.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- AdvancedA function accepts marks from 0 to 100. Which inputs best test the boundaries?