Skip to content
mentoor

Topic 1 of 5 · 4 min read

Approved tests as the agent's contract

Goal: Write or approve failing tests that encode a spec's acceptance criteria and lock them before the agent implements.

Before the agent writes any code, write or approve the tests that define "done", then lock them so the agent cannot change them. The tests become the contract. The agent's job is to meet it.

Check yourself · 1 of 3

Why write and approve tests before the agent writes the code?

Choose one answer

Why it matters

An agent such as Claude Code, Codex, Cursor or Copilot will happily write the code and the tests in one go. Then the tests only describe what the code already does. If the code has a wrong idea, the tests agree with it, and everything is green. Tests written first, from the spec, are the one check that does not come from the same guess.

How to do it

  1. Start from the acceptance criteria. Take each "given, when, then" line from the spec. One criterion becomes one or more tests.
  2. Write the tests against the interface, not the internals. Name the function, its inputs and its outputs. Do not describe how it works inside. Then the agent is free to choose the design.
  3. Run them and see them fail. A new test must fail before any code exists. A test that passes on an empty implementation proves nothing.
  4. Read and approve every test. This is the human step. Ask: does each test match a criterion, and is each expected value right? A wrong expected value is a wrong requirement.
  5. Lock them. Commit the tests on their own. Tell the agent in the prompt, and in the project's instruction file, that the test files are read-only for this task. Where you can, enforce it: a permission rule, a protected path in CI, or a check that fails if the test files changed.
  6. Then give the agent the goal. "Make these tests pass without editing them. Run them and show the output."

Before and after

Weak prompt: "Add a discount function and write tests for it."

Better: you write this first.

// discount.test.ts  (approved, committed, locked)
import { discount } from "./discount";

test("10% off orders of 100 or more", () => {
  expect(discount(100)).toBe(90);
});

test("no discount below 100", () => {
  expect(discount(99.99)).toBe(99.99);
});

test("rejects negative totals", () => {
  expect(() => discount(-1)).toThrow("invalid total");
});

Then: "Create discount.ts so these tests pass. Do not edit discount.test.ts. Run the tests and paste the result."

What goes wrong

  • Tests that mirror the code. If the agent wrote them after the code, they confirm its behaviour, right or wrong.
  • Approving without reading. A green light on tests you never read is a contract nobody signed.
  • An unlocked suite. The agent may edit a failing test to make it pass. A rule in the prompt helps. A check that fails the build is better.

Examples

One criterion, one test

The spec says: "Given a cart total of 100 or more, when checkout runs, then 10% is taken off." The approved test calls discount(100) and expects 90. It does not mention how the discount is computed, so the agent may choose any design that gives 90.

The lock in the prompt

"The files in tests/discount/ are approved and read-only. Implement src/discount.ts until npm test -- discount passes. If a test looks wrong, stop and tell me; do not change it." The last sentence gives the agent a safe way out when it disagrees.

Key points

  • Tests written from the spec before the code are the only check that does not share the agent's guesses.
  • Each acceptance criterion maps to at least one test, written against the interface and not the internals.
  • A new test must be seen failing before any implementation exists.
  • A person reads and approves every expected value, because a wrong expected value is a wrong requirement.
  • Lock the test files with a separate commit and, where possible, a check that fails if they change.

Common mistakes

  • Letting the agent write the code and the tests together, so the tests only confirm the code.
  • Approving tests by their names without reading the expected values.
  • Writing tests that reach into private details, which forces one design on the agent.
  • Telling the agent not to touch the tests but running nothing that would notice if it did.

Check yourself · 2 of 3

You wrote a new test and it passes before any code exists. What does that tell you?

Choose one answer

Check yourself · 3 of 3

You want the agent to implement against approved tests. Which instruction is best?

Choose one answer

Practice items, not counted towards your mastery.

Start here