Reviewer · Testing
Assert specific behavior
Tests must assert the exact behavior they claim to verify, not just a shape or structural property that can be satisfied vacuously or by accident. Several reviewed tests passed on both correct and buggy code because their assertions were too weak:
Tests must assert the exact behavior they claim to verify, not just a shape or structural property that can be satisfied vacuously or by accident. Several reviewed tests passed on both correct and buggy code because their assertions were too weak:
- Looping over a collection and asserting properties of each element (e.g.
for message in call["messages"]: assert isinstance(message["content"], str)) passes trivially if the collection is empty. Always assert the collection is non-empty first:assert call["messages"], f"{call['agent']} got no turns". - Checking only “is present” without checking “nothing unexpected is present” (e.g. asserting an expected turn exists but never asserting the exact expected count/content, so an extra duplicated or malformed row goes unnoticed).
- Using an over-broad match (
text.startswith(worker) and ": " in text) can accept false positives like"W1 said something: unrelated". Pin the exact expected shape, e.g. with a regex:re.match(rf"^{worker}(?: \(layer \d+/\d+\))?: ", text). - Indexing into a result (
calls[-1],agg_calls[0]) before asserting the call actually happened turns a real regression into a confusingIndexErrorinstead of a clear failure. Guard first:assert agg_calls, "aggregator was never invoked". - When two parallel tests intentionally check different things (e.g. one asserts only payload, another also asserts identity), make the asymmetry explicit via naming or a comment so a future reader doesn’t assume coverage that isn’t there.
When writing or reviewing a test, ask what mutation of the code under test would make it fail, and confirm the assertions actually catch that mutation rather than merely checking types, non-null-ness, or loose substrings.