Unit Tests: What to Test and What to Skip
Coverage is not quality. A good suite is small, fast, and fails for exactly one obvious reason.
Most test suites fail in one of two directions. Some have almost no tests, and every change is a small act of faith. Others have hundreds of tests that break constantly, take twenty minutes to run, and are quietly skipped by everyone on the team.
Both are expensive. The second is more expensive, because it looks like safety.
What a test is for
A test has exactly two jobs: catch a regression, and document what the code is supposed to do. If a test does neither, it is not a test β it is a maintenance cost with a green tick.
That definition settles most arguments. A test that would not fail if the behaviour broke is not protecting anything. A test that nobody can read is not documenting anything.
Test behaviour, not implementation
The most common mistake is testing how the code works rather than what it does. A test that calls a private helper, or asserts that an internal method was called twice, breaks when you rename or restructure β even though the behaviour never changed.
That kind of test punishes refactoring, which is the opposite of what you want. Tests should make change safe; these make change expensive.
Test through the public interface, with real inputs and real outputs wherever possible.
The shape of a good test
Three blocks, in order: arrange the world, perform one action, assert one outcome. If a test needs two actions, it is usually two tests.
The name should describe the behaviour, not the method. "returns zero for an empty cart" is a specification. "test_cart_2" is a serial number.
When it fails, the name plus the assertion should tell you what broke without opening the file. That is the whole value of the name.
What to skip
Trivial getters and setters. Third-party libraries β they have their own tests. Code you are about to delete. Configuration files.
And stop chasing a coverage number. Coverage tells you which lines ran, not whether the result was correct. A test that executes a function and asserts nothing raises coverage by exactly as much as a real test, and protects nothing.
A useful rule: if a bug could not plausibly live in this code, the test is ceremony.
What to always test
Boundaries. Empty, one, many, maximum, negative, null, and the off-by-one at each edge. Most bugs live at the boundary, because that is where the assumptions change.
Error paths. What happens when the network fails, the file is missing, the input is malformed, the permission is denied. These paths are rarely exercised by hand and are exactly where production breaks.
And every bug you fix. That test is the only one in the suite guaranteed to have a real failure behind it, which makes it the highest-value test you will ever write.
Mocks, at the edges only
Mock the things you do not control: the clock, the network, the filesystem, the payment provider. Those are slow, non-deterministic, or expensive.
Do not mock your own classes to test your own classes. If a test needs four mocks to run, it is usually testing the wiring rather than the behaviour, and the wiring will change next week.
Speed and flakiness
A slow suite stops being run. A flaky suite is worse: it teaches people that a red build is normal, and after that the suite has no authority at all.
The rule is blunt. A flaky test gets fixed or deleted the day it is identified. Never skipped indefinitely, never marked "known failure" for a quarter.
How many tests
Fewer than you think, in the right places. A common modern shape is the testing trophy: static analysis at the base, a thin layer of unit tests, a large layer of integration tests, and a small layer of end-to-end tests.
The right number is not a target. It is whatever catches your actual bugs at a speed the team will actually run.
The bottom line
Test behaviour through public interfaces, name tests after the behaviour, cover boundaries and error paths, and mock only at the edges. Delete anything that does not catch a regression or document a rule. Clean Code and The Pragmatic Programmer both treat tests as part of the design, not as an afterthought bolted on at the end.
π Recommended reading
Print editions of books covered on this site that go deeper into this topic.
See all 10 books ranked in βBest Programming Books for Developersβ β