Test-only branches in production code; code shaped to pass a test
Tests and verification: one of the ways AI coding agents bloat software, from lessloc's pattern catalog. lessloc catches it as test-logic.
What makes test-only branches in production code a bad practice?
The standards that call it a bad practice: Meszaros: Test Logic in Production; Anthropic system cards: special-casing tests.
What does lessloc do about test-only branches in production code?
lessloc catches it as test-logic.
test-logic(tier 1): Production code that checks whether tests are running, says it exists to make tests pass, or branches on a value that appears only in tests.
Examples
Caught as test-logic:
python
import os
def price(x):
if "PYTEST_CURRENT_TEST" in os.environ:
return 0
return xWritten well:
python
import os
def price(x):
return x * float(os.environ.get("RATE", "1"))Caught as test-logic:
js
export function price(x) {
// special case for the test fixture
if (x === 42) return 0;
return x * 2;
}Written well:
js
export function price(x) {
// Prices are stored in cents: 42 means $0.42
return x * 2;
}Related patterns
The other patterns in Tests and verification:
- Behavior added with no test changed in the same change: lessloc gives every session the rule: new behavior gets one focused test.
- Tests that assert nothing, or assert a constant: lessloc catches it as
empty-test. - Deleted, skipped or force-passed tests: lessloc catches it as
weakened-tests. - Tests that mock the code they test: lessloc catches it as
empty-test. - Production code that branches on a value from the tests: lessloc catches it as
test-logic. - Stopping short: a requested function never written: lessloc catches it as
unaddressed: names a request asks to exist ("add a--statusflag", "implementexport_csv()") are looked for in the final repository; one that appears nowhere holds the turn. - Stopping short: part of the problem left failing: lessloc catches it as
unfinished: in a turn that changed code, the agent's last reply is read sentence by sentence, with headings and lead-ins speaking for what is under them. - Saying "done" without a passing run on the final state: lessloc catches it as
tests-failedandunverified: the agent's own test runs, as its host reported them, are the evidence for the exact tree; a run that fails on the final state, or a change to how the tests run, holds the turn once, and a final state no run verified is written to the record;lessloc test,verifyandcirecord evidence too. - A refactor that changes existing tests: lessloc catches it as
refactor-tests.

