Testing checks whether software behaves as expected in specified scenarios. A traditional test case defines a starting state, supplies inputs, and asserts an expected result. Tests make changes less risky, but a passing case establishes only that its particular scenario passed—not that the whole system is correct. section 5.2.2 section 5.0
A useful test suite needs more than passing results:
- Check the intended behavior. A test based on a misunderstanding can pass while documenting behavior that does not exist; a flawed assertion can miss a bug it was meant to catch. section 5.2.1
- Keep expectations simple. Prefer a literal expected value to one calculated with logic that might repeat the implementation’s mistake. section 5.7
- Look beyond a coverage percentage. Line coverage can miss an untested branch, while a high target can reward weak tests written merely to reach it. section 5.7
Unit tests check individual pieces of code and offer quick, local feedback. Aim for tests that are focused, understandable, deterministic, isolated, and informative when they fail. Name each test for the behavior and expected outcome, and keep it narrow enough to serve as readable documentation. Check observable behavior rather than private methods or internal state so unrelated implementation changes do not break it. section 5.3.11 section 5.4 section 5.7
Testing an array sort
For a sorting function, test an unsorted array, an already sorted array, a one-element array, and an empty array. These are distinct scenarios, not proof that every possible input works. When the expected behavior is clear, they can also guide a Red-Green-Refactor cycle: observe a failing test, write enough code to pass it, then improve the implementation while keeping it green. section 5.6 section 5.2.2
When hand-picked cases leave too much room for missed edge cases, property-based testing checks invariants, such as debits equaling credits. It complements rather than excuses careful examples. Integration tests matter too, but they tend to run more slowly and can make failures harder to diagnose than unit tests. Add tests when changing existing code; writing them before implementation is an optional workflow, not a measure of quality by itself. section 5.7 section 5.3.5 section 5.3.8


































































