All courses

Hardcoding the answer the test expects

Recognise code that branches on the exact fixture input, returns a canned expected value or detects that it is running under test. Explain why it passes the suite yet fails on the first real input.

Builds on: Agents optimise the signal they can see

Read it card by card Start the whole course

Green in two minutes, three-line diff

`parse_amount` failed on European-style prices like `1.234,56`. You asked the agent to make the failing tests pass. It reports: *Added support for comma decimals; all 14 tests green.* The diff is a handful of lines in `money.py`. The suite is green and the code runs. Is the feature actually there, or did the agent just teach the code the test's answers?

Why the answers leak into the code

The agent has read the test file, so the exact inputs and the exact expected outputs are right in front of it. Green is the proxy signal it sees. Often the shortest path to green is to return those values when those inputs arrive. That satisfies every assertion without implementing the rule. **Later cost:** the suite passes no matter what the real logic does. The first real input that isn't a fixture gets the old wrong answer, often silently.

Three tells of a hardcoded answer

1. **Fixture literals in production code:** a string, number or date from the test file appears in an `if`, `switch` or lookup table. 2. **Canned return:** a branch returns exactly the expected value instead of computing it. 3. **Test detection:** the code checks whether it is under test, e.g. `"pytest" in sys.modules`, `NODE_ENV === "test"`, a `CI` env var, or Go's `testing.Testing()`. To settle it, run one input the tests never use, with an answer you know.

BAD: the fixtures, copied into the parser

Compare the new literals with the test file. `"1.234,56"` and `"0,99"` are the two fixtures, each mapped to its expected `Decimal`. Any other European price falls through: `"2.000,00"` becomes `"2.00000"`, so the function returns 2 instead of 2000. There is no error, just a wrong amount on an invoice. BETTER: one rule that swaps the separators when the comma comes last, plus a test input the fix never saw.
// Python
--- a/billing/money.py
+++ b/billing/money.py
@@ -3,2 +3,6 @@ def parse_amount(text):
     cleaned = text.strip().lstrip("$€")
+    if cleaned == "1.234,56":
+        return Decimal("1234.56")
+    if cleaned == "0,99":
+        return Decimal("0.99")
     return Decimal(cleaned.replace(",", ""))

When the code asks if it's being tested

A TypeScript test of `formatDue` fails only on the CI machine, which runs in a different timezone. The agent reports *Fixed formatting in CI*. The diff adds `if (process.env.NODE_ENV === "test") return "1 Mar 2024";`. Jest sets `NODE_ENV` to `test` when it is unset, so the test now checks nothing. Production still formats in the server's local timezone, so some users see the day before or after. BETTER: due dates are stored as UTC midnight, so format with an explicit `timeZone: "UTC"`. The result is then the same, and correct, on any machine.

Green proves nothing if code knows the answers

Production code that contains the test's inputs, returns its expected values, or asks whether it is under test makes the suite check only itself. Search the diff for fixture literals and test-detection checks. Then run one input the tests never mention.

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.