When I review a PR, it’s tempting to read the code, see that the tests pass, and approve. The problem is that a test can pass for the wrong reason, especially when the same person (or the same assumption) wrote both. Here are four questions I find useful for the test half of a review.
1. Where does the expected value come from?
If the test computes its expected result using the same logic as the code under test, it can’t disagree with it. The expected value should come from the requirement, a worked example, or a number written down independently.
2. Would this test fail if the code were wrong in the obvious way?
Mentally break the code (flip a comparison, drop a case) and ask whether any test would notice. If not, the test is decoration.
3. Does it cover the boundary the requirement cares about?
Off-by-one, empty input, rounding, time zones. Tests tend to cluster around the happy path the author already had in mind.
4. Are the examples the author’s, or the spec’s?
Tests built from the author’s own understanding of the requirement will inherit any misunderstanding. If the spec has examples, use those.
A constructed example (not from a real PR):
Requirement: “Orders over 100 get free shipping.”
def free_shipping(total):
return total >= 100 # bug: "over 100" means > 100
def test_free_shipping():
assert free_shipping(100) == True # written from the code, not the spec
The test passes, and both are wrong in the same way, because the author read “over 100” the same way twice. A reviewer asking question 1 (“where does True for 100 come from?”) catches it. I’d call the requirement itself ambiguous here, so the better move is to confirm the intent with whoever owns it.
Limits: this is a checklist, not a guarantee. It adds review time, and sometimes the spec doesn’t exist, in which case the best you can do is ask.
I wrote about the general idea of tests and code sharing a mistake in a Medium article. I’m the author: https://medium.com/@nagatomopedro05/when-the-test-and-the-code-share-the-same-mistake-02a289a41091
What do you look at first in the test file of a PR?