Lesson 48 of 93 · 5 min read · Updated
Balancing Unit Test Qualities: Trade-offs Between Trivial, Brittle and E2E Tests
On this page
The previous lesson introduced four qualities of a good unit test. This lesson covers the hard part: you cannot get the maximum of all four in a single test. You will see where common test types sit on that trade-off, look at a low-value test that inflates the suite, and learn why the balance belongs to the whole test portfolio rather than to each test.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- FluentAssertions 7.2.2 (Apache-2.0; 8.x is commercial)
- The
LoyaltyPointsCalculatorclass from the previous lesson
No Test Maximizes All Four
The four qualities pull against each other. Regression protection grows with the amount of real code a test exercises. Fast feedback shrinks with it. A test that runs the whole application, database and browser included, catches many kinds of bugs, but it takes seconds or minutes and breaks for reasons that have nothing to do with your change.
Here is where typical tests land:
| Test type | Regression protection | Resistance to refactoring | Fast feedback | Maintainability |
|---|---|---|---|---|
| End-to-end test | High | High | Low | Low |
| Trivial unit test (checks a getter or constant) | Low | High | High | High |
| Brittle unit test (checks internals) | High | Low | High | Medium |
| Behavior-focused unit test | High | High | High | High (for domain logic) |
The last row looks like a winner, and for domain logic it is the best balance available. But it only works for code that has interesting logic and few dependencies, such as the loyalty points calculator. For code that mainly talks to a database or an HTTP API, a unit test cannot exercise the part where most bugs live, so you need integration tests there, and they pay for that protection with speed.
Khorikov makes one more point: resistance to refactoring is the quality you should not trade away. A test either couples to implementation details or it does not; there is little middle ground. The real trade-off is between regression protection and fast feedback, and you choose it per test type.
A Low-Value Test
Not every test earns its place. Here is a test that is fast, readable and stable, and still adds almost nothing:
M06.BestPractices.Tests/FourQualitiesTests.cs
The test repeats the value of a constant. Ask the first question from the previous lesson: if I break the code, does this test fail? Change amountSpent < MinimumSpend to amountSpent <= MinimumSpend and customers who spend exactly 100 lose their points. This test stays green. Remove the threshold check altogether and it stays green again. The only change it catches is an edit to the constant itself, and that edit is usually deliberate.
Compare it with the two boundary rows of the behavior test:
M06.BestPractices.Tests/FourQualitiesTests.cs
These two rows catch a changed constant, a changed comparison operator and a deleted check. They protect the rule "spending of 100 or more earns points", which is what the business cares about. The constant test protects a number.
Should You Delete the Trivial Test?
Usually, yes. A test that protects nothing still costs something: it runs on every build, it shows up in reviews, and it adds to a test count that makes the suite look stronger than it is. That false sense of safety is the real harm.
Before you delete it, ask whether the value is a contract that something outside the class depends on. If MinimumSpend is shown to customers in the app, printed in the terms and conditions, or read by another system, then a change to it is a business decision that someone should notice. In that case the test has a job, but it should say so. A name such as MinimumSpend_MatchesThePublishedTerms tells the next reader why the test exists. Without such a reason, the boundary rows already cover the rule, and the constant test is redundant.
Balance the Portfolio, Not the Single Test
Since no single test can be perfect, aim for a suite where each kind of test does the job it is good at:
- Many behavior-focused unit tests for domain logic. They are fast and stable, and they protect the rules where most business bugs live.
- Fewer integration tests for code that talks to databases, queues and external APIs. They are slower, but they catch bugs that unit tests cannot reach.
- A small number of end-to-end tests for the most important user journeys. They give the highest confidence and the slowest feedback.
- No trivial tests that only repeat constants, getters or framework behavior.
When you review a test, use the four qualities as a checklist:
- If the code under test breaks, does this test fail?
- If I refactor the code without changing its behavior, does this test stay green?
- Is it fast enough to run after every change?
- Can a teammate read it and see what rule it protects?
A test that fails the first question is a candidate for deletion or replacement. A test that fails the second one is a source of false alarms and needs to move from internals to the public API. The next lesson looks at that second kind of failure in detail.
Summary
- The four qualities conflict; no single test can maximize all of them.
- End-to-end tests give high protection but slow feedback; trivial unit tests are fast but protect almost nothing.
- Treat resistance to refactoring as non-negotiable and trade protection against speed per test type.
- A test that only repeats a constant or getter inflates the suite and gives a false sense of safety; replace it with boundary tests of the rule.
- Keep such a test only if the value is an external contract, and name the test after that contract.
- Balance the whole portfolio: many behavior unit tests, fewer integration tests, a few end-to-end tests.