Reviewer · Testing
Discriminating test assertions
A test that passes under a plausible bug is worse than no test: it hides the regression it claims to cover. Before trusting a new test, ask "what incorrect implementation would still make this pass?" and change the inputs or assertions until no such implementation survives.
A test that passes under a plausible bug is worse than no test: it hides the regression it claims to cover. Before trusting a new test, ask “what incorrect implementation would still make this pass?” and change the inputs or assertions until no such implementation survives.
Common ways tests lose their bite:
- Picking inputs that can’t tell two behaviors apart. A
holdsEveryEntry-style check needs a case where “any entry held” and “every entry held” diverge; a tenant-scoped boundary (e.g.max_query_lookback) needs two tenants with different values, not the same one. - Asserting counts or presence instead of the actual values that matter (e.g. assert the exact
Start/Enda downstream mock receives, not just that a call happened). - Using the wrong assertion for identity:
require.Equalon a pointer compares the pointee, not the address, sorequire.Same(or an equivalent pointer check) is required to prove two values are shared, not just equal. - Relying on an accidental default or coincidental ordering (e.g. a config flag that’s off by default, alphabetical sort of tenant names) rather than explicitly setting/naming things so the test exercises the real logic.
- Fakes/mocks that accept anything (e.g. a flusher that doesn’t check builder identity, a deny-list fake that ignores its tenant argument) — make fakes differentiate per input (per-tenant behavior, pointer equality, call arguments) so swapped or reordered calls are caught.
- Boundary and combination gaps: test the exact threshold value, one value on each side of it, combinations of conditions (e.g. a 400 validation error together with a 429 rate limit, two merges skipped in the same run, several small values that individually pass a limit but sum above it).
- Config values chosen so the code path under test never actually engages (e.g. a target size large enough that scratch space is never touched, masking a missing
defer Reset()).
When adding a regression test, briefly revert the fix (or reason through the old code) and confirm the new test fails on it. Prefer asserting the externally observable outcome (final ordering, stored values, emitted metrics) over trusting an implementation’s internal accounting, and favor a differentiating fake/table case over a looser, implementation-trusting one.
Example — asserting sharing requires identity, not equality:
// Wrong: passes even if sortPrefix never shares memory.
require.Equal(t, unsafe.StringData(first.SchemaKey), unsafe.StringData(second.SchemaKey))
// Right: fails unless the two calls actually share the same backing array.
require.Same(t, unsafe.StringData(first.SchemaKey), unsafe.StringData(second.SchemaKey))