Skip to content

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 x

Written 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;
}

The other patterns in Tests and verification:

Released under the Apache-2.0 license. · llms.txt