Lesson 47 of 93 · 6 min read · Updated
The Four Qualities of a Good Unit Test: Protection, Resistance, Speed, Maintainability
On this page
A green test is not always a useful test. Some tests catch real bugs, some raise false alarms every time you refactor, and some protect nothing at all. This lesson gives you four qualities to judge any unit test against, and shows a test for a loyalty points calculator that scores well on all four. You will use these four qualities as a yardstick in every lesson that follows.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- FluentAssertions 7.2.2 (the 7.x line is Apache-2.0; version 8.x requires a commercial licence for commercial use)
- A class library
M06.BestPracticesand a test projectM06.BestPractices.Teststhat references it
Run the tests with:
The Four Qualities
The framework comes from Vladimir Khorikov's book "Unit Testing: Principles, Practices, and Patterns". It describes a test by four qualities:
| Quality | What it means | Question to ask |
|---|---|---|
| Protection against regressions | The ability to catch bugs | If I break the code, does this test fail? |
| Resistance to refactoring | No false alarms when the internal structure changes | If I restructure the code but keep the behavior, does this test stay green? |
| Fast feedback | How long the test takes to run | Can I run it after every small change? |
| Maintainability | The cost to read and change the test | Can a teammate understand it in under a minute? |
The first two qualities deal with two different kinds of error. A test with weak regression protection gives you a false negative: the code is broken, but the test stays green. A test with weak refactoring resistance gives you a false positive: the code still works, but the test turns red because you renamed a private field or swapped a list for a dictionary.
Both errors are expensive. False negatives let bugs reach production. False positives are slower poison: after the tenth false alarm, the team stops trusting red builds and starts "fixing" tests by updating the expected values. At that point the suite no longer protects anything.
Fast feedback and maintainability are about cost. A test that takes 30 seconds will not be run after every edit. A test that needs ten minutes of reading before you can change it will be deleted the first time it gets in the way.
The Code Under Test
The examples in this section use a static calculator with several branches and boundaries. That makes it a good subject: reaching 100% line coverage is easy, but protecting the behavior requires sampling each boundary on purpose.
M06.BestPractices/LoyaltyPointsCalculator.cs
The method has five rules, and each rule is a place where a bug can hide: the threshold check, the division, the tier multiplier, the campaign multiplier and the upper cap.
A Test That Scores Well on All Four
Here is a data-driven test that samples every meaningful branch and boundary:
M06.BestPractices.Tests/FourQualitiesTests.cs
Go through the qualities one by one:
- Protection against regressions. Each rule has a row that fails if the rule breaks. Change
<to<=in the threshold check and the100.00row fails. Change the Silver multiplier and the125row fails. RemoveMath.Minand the5_000row fails. - Resistance to refactoring. The test calls only
Calculateand checks only its return value. You can replace theswitchwith a dictionary lookup or split the method into helpers, and the test stays green as long as the behavior stays the same. - Fast feedback. There is no database, no file system and no network. Seven rows run in a few milliseconds.
- Maintainability. Every row reads like a line from a specification: input, tier, campaign flag, expected points. The expected values are written as literals, so a reader can check them with mental arithmetic.
The Error Path Counts Too
Regression protection is not only about the happy path. The rule "spending cannot be negative" is also behavior, and someone can break it by deleting the guard clause.
M06.BestPractices.Tests/FourQualitiesTests.cs
The test checks the exception type, not the message text. The message is a detail that may change for wording reasons; the exception type is part of the contract that callers depend on. Asserting on the message would lower resistance to refactoring for no real gain in protection.
A common mistake is to stop at one happy-path row, such as 1_000, Standard, false, 100. That test is just as fast and just as readable, but it would stay green if the threshold, the tier bonuses, the campaign rule or the cap were broken. Speed and readability are cheap to get. Protection is the quality you have to design for.
Summary
- Judge a unit test by four qualities: protection against regressions, resistance to refactoring, fast feedback and maintainability.
- Weak protection causes false negatives (missed bugs); weak refactoring resistance causes false positives (false alarms).
- False alarms destroy trust in the suite, so treat resistance to refactoring as seriously as protection.
- Test through the public API and assert on observable results to stay resistant to refactoring.
- Sample every branch and boundary with explicit expected values to get real protection.
- Error paths are behavior too; assert on the exception type rather than the message.