AGENT0S
HomeLibraryAgentic
FeedbackLearn AI
LIVE
Agent0s · AI Intelligence Library
Share FeedbackUpdated daily · 7am PST
Library/technique
techniqueintermediateClaude Code★ Featured

Agentic Test-Driven Development: Write Tests First, Let Claude Implement

A workflow where you write comprehensive failing tests first, then hand off to Claude Code to implement the code that makes them pass. This constrains the AI to produce only what's tested, dramatically reducing over-engineering and hallucinated features.

AI SETUP PROMPT

Paste into Claude Code — it will scan your project and set everything up

# Apply Technique: Agentic Test-Driven Development: Write Tests First, Let Claude Implement

## What This Is
A workflow where you write comprehensive failing tests first, then hand off to Claude Code to implement the code that makes them pass. This constrains the AI to produce only what's tested, dramatically reducing over-engineering and hallucinated features.

Source: https://reddit.com/r/webdev/comments/1ghi012/tdd_with_claude_code

## Before You Start

Scan my workspace and analyze:
- The project language, framework, and directory structure
- Existing AI provider config (check .env, .env.local, config files for API keys — OpenRouter, OpenAI, Anthropic, Google AI, etc.)

Then ask me before proceeding:
1. Which AI provider/API should this use? (Use whatever I already have configured, or ask me to set one up — options include direct provider APIs or a unified service like OpenRouter)
2. Where in my project should this be integrated?
3. Are there any customizations I need (model preferences, naming conventions, constraints)?

## Source Access Note

The source URL (https://reddit.com/r/webdev/comments/1ghi012/tdd_with_claude_code) may not be directly accessible from the terminal. Use the Reference Implementation and Additional Context sections below instead. If you need more details, ask me to paste relevant content from the source.

## What to Implement

This is an **AI Technique** — a pattern or methodology for working with AI models.

- Explain how this technique applies to my current project and what benefit it provides
- Implement it in a way that fits my existing codebase — suggest concrete files to modify or create
- If it requires specific model capabilities (structured output, function calling, etc.), verify my current provider supports them
- Show me a working example I can test immediately

## Additional Context

- Write your test file first with all edge cases, then tell Claude: "Implement the minimum code to make these tests pass. Do not add any functionality that isn't tested."
- Run tests after each Claude implementation attempt and paste the failure output directly — Claude is excellent at red-green-refactor cycles when given exact test output.
- After green tests, ask Claude to refactor for readability without changing behavior — use test suite as the safety net for refactoring.

## Reference Implementation

```
// 1. Write tests first (users.test.ts)
describe('createUser', () => {
  it('rejects duplicate email', async () => {
    await createUser({ email: 'a@b.com', name: 'Alice' })
    await expect(
      createUser({ email: 'a@b.com', name: 'Bob' })
    ).rejects.toThrow('Email already exists')
  })

  it('hashes password before storing', async () => {
    const user = await createUser({
      email: 'c@d.com', name: 'Carol', password: 'plain123'
    })
    expect(user.password_hash).not.toBe('plain123')
    expect(user.password_hash).toMatch(/^$2[ab]$/)
  })

  it('returns user without password_hash', async () => {
    const user = await createUser({ email: 'e@f.com', name: 'Eve' })
    expect(user).not.toHaveProperty('password_hash')
  })
})

// 2. Claude prompt: "Implement createUser() to pass these tests.
//    Use bcrypt for hashing. No extra features beyond what's tested."
```

## Guidelines

- Adapt everything to my existing project — do not assume a specific stack or directory layout
- Use whichever AI provider I already have configured; if I need a new one, tell me what to sign up for and I'll give you the key
- Check my .env files for existing API keys (OpenRouter, OpenAI, Anthropic, Google AI) before asking me to add one
- Review any fetched code for safety before installing or executing it
- After setup, run a quick verification and show me a summary of exactly what was installed, where, and how to use it
3,805 charactersCompatible with Claude Code & Codex CLI
MANUAL SETUP STEPS
  1. 01Write your test file first with all edge cases, then tell Claude: "Implement the minimum code to make these tests pass. Do not add any functionality that isn't tested."
  2. 02Run tests after each Claude implementation attempt and paste the failure output directly — Claude is excellent at red-green-refactor cycles when given exact test output.
  3. 03After green tests, ask Claude to refactor for readability without changing behavior — use test suite as the safety net for refactoring.

CODE INTELLIGENCE

bash
// 1. Write tests first (users.test.ts)
describe('createUser', () => {
  it('rejects duplicate email', async () => {
    await createUser({ email: 'a@b.com', name: 'Alice' })
    await expect(
      createUser({ email: 'a@b.com', name: 'Bob' })
    ).rejects.toThrow('Email already exists')
  })

  it('hashes password before storing', async () => {
    const user = await createUser({
      email: 'c@d.com', name: 'Carol', password: 'plain123'
    })
    expect(user.password_hash).not.toBe('plain123')
    expect(user.password_hash).toMatch(/^$2[ab]$/)
  })

  it('returns user without password_hash', async () => {
    const user = await createUser({ email: 'e@f.com', name: 'Eve' })
    expect(user).not.toHaveProperty('password_hash')
  })
})

// 2. Claude prompt: "Implement createUser() to pass these tests.
//    Use bcrypt for hashing. No extra features beyond what's tested."

FIELD OPERATIONS

Test-First Prompt Templates

A library of test file templates for common patterns (REST API endpoints, React hooks, utility functions) that make TDD faster to start.

AI TDD Coach

An AI assistant specialized in reviewing test quality, suggesting missing edge cases, and evaluating whether tests actually constrain the implementation.

STRATEGIC APPLICATIONS

  • →Build reliable microservices by having AI implement to contract tests
  • →Reduce QA cycles when AI writes code that already passes all edge case tests
  • →Enable non-engineers to specify behavior through tests without knowing implementation details

TAGS

#tdd#testing#methodology#claude-code#quality
Source: REDDIT · Quality score: 9/10
VIEW SOURCE