principleMajorpending
Test behavior not implementation -- resilient test design
Viewed 0 times
behavior testingimplementation detailrefactoringpublic APItest resiliencemock vs fake
Problem
Tests that verify internal implementation details (private methods, internal state, call counts) break every time you refactor, even when behavior is unchanged. This makes refactoring expensive and tests feel like a burden.
Solution
Test the public interface and observable behavior: given inputs, assert outputs and side effects. Do not test: which private methods were called, the order of internal operations, internal data structures, or implementation details of dependencies (use fakes over mocks). Tests should be written so that you can completely rewrite the internals and the tests still pass if behavior is preserved.
Why
Tests exist to catch regressions in behavior. If they break during refactoring (same behavior, different implementation), they are testing the wrong thing and actively harm development velocity.
Revisions (0)
No revisions yet.