Tech & AI

Unit Tests: What to Test and What to Skip

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.

πŸ“š
Recommended reading: the ideas here are covered in depth in Clean Code. Check the book β†’

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” β†’

Related articles

Practical Prompt Engineering for AI Tools

Practical Prompt Engineering for AI Tools

Better prompts are less about magic words and more about clear thinking. Here are the habits that reliably get more useful answers from AI tools.

πŸ“ Tech & AI πŸ—“ September 30, 2026 ⏱ 8 min read
Using ChatGPT at Work Without Losing Your Edge

Using ChatGPT at Work Without Losing Your Edge

AI assistants can save real hours each week β€” if you use them for the right tasks. A practical guide to weaving them into everyday work.

πŸ“ Tech & AI πŸ—“ October 1, 2026 ⏱ 8 min read
Cybersecurity Basics: Protecting Your Accounts and Devices

Cybersecurity Basics: Protecting Your Accounts and Devices

Most breaches do not start with brilliant hackers. They start with a reused password, an ignored update, or a link that should not have been clicked. Here is how to close those gaps.

πŸ“ Tech & AI πŸ—“ October 2, 2026 ⏱ 8 min read

Suggested summaries

Tech & AI The Innovator's Dilemma book cover

The Innovator's Dilemma

Clayton M. Christensen Β· 1997
β˜…β˜…β˜…β˜…β―¨ 4.5

Why do well-managed companies lose to small entrants with worse products? Christensen's answer is that the same decisions that make them successful are the problem.

Tech & AI
Tech & AI Homo Deus book cover

Homo Deus

Yuval Noah Harari Β· 2016
β˜…β˜…β˜…β˜…β―¨ 4.5

Harari looks past famine, plague and war to ask what humanity will chase next: immortality, engineered happiness and god-like power over life itself.

Tech & AI
Tech & AI Life 3.0 book cover

Life 3.0

Max Tegmark Β· 2017
β˜…β˜…β˜…β˜…β―¨ 4.5

Max Tegmark maps the choices ahead for artificial intelligence β€” utopia, catastrophe or something in between β€” and why values matter as much as code.

Tech & AI