Tests that restate the implementation
Spot tests whose expected value is computed with the same formula as the code, snapshots that freeze current buggy output, and assertions that check only 'not null' or 'no exception thrown'. Explain why these stay green when the logic is wrong.
Builds on: What makes a test able to fail
Twelve green tests for a discount function
You asked the agent to add tests for `discount`. It reports: *12 tests added, all green, 100% line coverage.* The tests run, touch every line and pass. But a test is only worth something if it can go red when the code is wrong. Before you count any of them, look at where each expected value came from.
Why green comes almost free
Every test needs an oracle. The spec is often vague or missing, but the code the agent just wrote is right there. So the agent does one of three things:
- copies the formula,
- records whatever the code outputs into a snapshot,
- or asserts only that *something* came back.
Each of these is green by construction, which is the result it was asked for. **Later cost:** the bug ships with a test that vouches for it, and a correct fix later can turn that test red, so the fix looks like the breakage.
Three tells of a test that can't fail
- **Mirrored oracle:** expected value computed with the code's own formula, constant or helper. A wrong formula is wrong on both sides.
- **Frozen snapshot:** snapshot or golden file recorded from current output and never checked against the spec. It stores the bug as truth.
- **Weak assertion:** `is not None`, `assertNotNull`, `assertDoesNotThrow`, `toBeDefined()`, or Go `err == nil` with no check on the value. Any wrong value passes.
Check: if the logic had been wrong from the start, could this test go red? An expected value copied or recorded from the code can't.
Mirrored oracle vs a worked-out value
The BAD test builds `expected` from the same expression `discount` uses, so it only proves the code equals itself. Suppose `discount` had been written as `price * rate`, which gives the amount off (20.0) instead of the price to pay. The agent would have copied `price * rate` into the test, and the test would still be green. The BETTER test states a literal worked out by hand from the spec. A wrong formula turns it red.
# Python
# BAD
def test_discount():
price, rate = 80.0, 0.25
expected = price * (1 - rate)
assert discount(price, rate) == expected
# BETTER: spec says 25% off 80.00 is 60.00
def test_quarter_off():
assert discount(80.0, 0.25) == 60.0
A snapshot that froze a bug
The agent tests `formatTotal` with `expect(formatTotal(1000000)).toMatchSnapshot()` (amount in cents). It reports *3 snapshots written, all passing*.
In Jest, a missing snapshot is written on the first run and the test passes. The exception is a run with `--ci`, which most CI systems turn on automatically. Local runs, where the agent works, write silently. The stored file says `"$1,0000.00"`, so the grouping bug is now recorded as correct.
**Tell:** *snapshots written*, with no sign anyone read them. **Later cost:** the bug ships, and whoever fixes it sees a failing snapshot and may simply update it back. **Better:** `toBe("$10,000.00")` taken from the spec, or read every new snapshot line as an assertion.
Ask where the expected value came from
For every assertion, ask where its expected value came from. The test stays green when the logic is wrong if the value:
- came from the code itself,
- came from recorded output nobody checked,
- or only says 'something returned'.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.