Skip to content

Tests that mock the code they test ​

Tests and verification: one of the ways AI coding agents bloat software, from lessloc's pattern catalog. lessloc catches it as empty-test.

What makes tests that mock the code they test a bad practice? ​

The standard that calls it a bad practice: Carr, TDD anti-patterns: The Mockery.

What does lessloc do about tests that mock the code they test? ​

lessloc catches it as empty-test.

  • empty-test (tier 1): A test that asserts nothing or only a constant, or one that mocks the code it tests.

Examples ​

Caught as empty-test:

python
from unittest.mock import patch


@patch("shop.pay.charge")
def test_charge(mock_charge):
    mock_charge.return_value = 1
    assert charge(5) == 1

Written well:

python
from unittest.mock import patch


@patch("shop.pay.send_receipt")
def test_charge(mock_send):
    assert charge(5) == 5
    mock_send.assert_called_once()

Caught as empty-test:

js
import { test, vi } from "vitest";
import { total } from "./cart";
vi.mock("./cart");
test("total", () => {
  expect(total([1])).toBe(1);
});

Written well:

js
import { test, vi } from "vitest";
import { total } from "./cart";
vi.mock("./prices");
test("total", () => {
  expect(total([1])).toBe(1);
});

The other patterns in Tests and verification:

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