# Make a coding agent write the failing tests first, then implement

> Split the work into separate prompts: tests only, confirm they fail for the right reason, commit, then implement without touching the tests.

- **Category:** [Coding](https://stepgleam.com/categories/coding)
- **Difficulty:** Intermediate
- **Time:** ~30 minutes
- **Tools:** Claude Code, Git
- **Reliability score:** 84/100
- **Steps:** 5
- **Last updated:** 2026-10-26
- **Published:** 2026-10-26

## Tools used

- [Claude Code](https://code.claude.com/docs) — Writes the tests, then the implementation
- [Git](https://git-scm.com) — Commits the failing tests as a baseline

## What you'll get

Committed failing tests, an implementation that makes them pass, and test files the agent did not edit.

## Who this is for

Developers using Claude Code, Codex or Cursor on code with a test runner.

## Before you start

Developers on r/ClaudeCode who work test-first with Claude Code agree: let the agent write tests and code in one go and it writes the code first, then back-fills tests that fit. The fix is separate prompts, and checking the red step yourself.

## Steps

### Step 1: Prompt for tests only

**Tool:** Claude Code

First prompt: tests for the behaviour, no implementation. One developer: *"first prompt is write the failing tests for X. dont implement."* Name the cases; Anthropic's guide contrasts "add tests for foo.py" with naming the edge case and saying to avoid mocks.

**Why this works:** Separate prompts stop the agent writing code first and tests to fit.

**Prompt:**

```
Write tests for the proration function in test/billing/: mid-cycle upgrade, mid-cycle downgrade, zero-length cycle, cancelled subscription. Follow the existing test style. Do not write the implementation. Run them and show me the failing output.
```

[Open step 1](https://stepgleam.com/guides/make-a-coding-agent-write-failing-tests-first#step-1)

### Step 2: Read the tests and confirm they fail for the right reason

**Tool:** Claude Code

*"run them and show me theyre red. (this is the step everyone skips, and claude WILL happily claim tests pass without running them if you let it.)"* Another developer reads the tests and confirms *"they fail for the right reason"*, not because of a typo or import error.

[Open step 2](https://stepgleam.com/guides/make-a-coding-agent-write-failing-tests-first#step-2)

### Step 3: Commit the failing tests

**Tool:** Git

Commit them so any later edit to the test files shows up in `git diff`. An r/ClaudeAI user's loop starts the same way: *"write failing tests first, git commit."*

**Prompt:**

```
git add test/ && git commit -m "Add failing tests for proration"
```

[Open step 3](https://stepgleam.com/guides/make-a-coding-agent-write-failing-tests-first#step-3)

### Step 4: Implement in a fresh prompt and forbid test edits

**Tool:** Claude Code

Second prompt: make the tests pass and don't modify the test file; if a test looks wrong, report why instead. Some clear the session first so the implementer meets the tests as a given.

**Prompt:**

```
Implement calculateProration so every test in test/billing passes. Do not modify any file under test/. If you think a test is wrong, explain why without changing it. Run the tests and paste the result.
```

[Open step 4](https://stepgleam.com/guides/make-a-coding-agent-write-failing-tests-first#step-4)

### Step 5: Make the rule stick

**Tool:** Claude Code

Add a line to CLAUDE.md: *"never write source code in the same turn as new tests."* Some developers also use a hook that blocks source edits when there's no matching test file.

[Open step 5](https://stepgleam.com/guides/make-a-coding-agent-write-failing-tests-first#step-5)

## Pro tips

- Ask for boundary cases explicitly; agents tend to test representative values only.
- Check git diff on the test folder after implementation.

## Common mistakes

- One prompt that says "write tests and implement it".
- Not reading the failing output yourself.
- Letting the agent edit tests to make them pass.

## FAQ

### Does this work in Cursor or Codex?

The DEV write-up says the same instructions work in each tool's context file.

### What if there is no test setup?

Ask the agent to propose one first.

---

Canonical guide: https://stepgleam.com/guides/make-a-coding-agent-write-failing-tests-first
Read this guide as Markdown: https://stepgleam.com/guides/make-a-coding-agent-write-failing-tests-first.md
