Good code needs few unit tests
“We write great code here, just look at how many unit tests we have!”
This meme needs to die. I touched on it earlier, now let’s make the formal argument:
- Units are abstractions which encapsulate a piece of functionality.
- A unit tests covers an intended use-case for the interface a Unit exposes.
- One quality measure of an abstraction is the complexity of its interface (ie API size).
- Another measure is the amount of state the abstraction encapsulates.
- Good abstractions have simple interfaces and as little state as necessary.
Hence, good abstractions require few unit tests. Conversely, a lots of unit tests are a symptom of an architecture with complex and highly stateful abstractions, which, in turn, are a sign of a low quality architecture.