Test-Driven Prompting

Test-driven prompting is a development approach where you write or generate tests before asking AI to implement functionality. By defining expected behavior through tests first, you create clear specifications that guide AI toward correct implementations and make verification automatic.

Example

Before implementing a discount calculator, you write tests: 'applies 10% discount for orders over $100', 'no discount under $100', 'caps at 25% max discount'. Then ask AI to implement code that passes all tests.

Test-driven prompting adapts Test-Driven Development (TDD) for AI-assisted workflows. Tests become specifications that guide AI generation and automatically verify correctness.

The Test-First Workflow

  1. Write tests first — Define expected behavior
  2. Share tests with AI — Tests become the specification
  3. Generate implementation — AI writes code to pass tests
  4. Run tests — Verify correctness automatically
  5. Iterate — Fix failures, add edge cases

Why Tests Make Better Prompts

Tests are precise specifications:

  • Unambiguous expected behavior
  • Clear input/output examples
  • Edge cases explicitly defined
  • Success criteria built-in

Compare:

  • Prompt: "Write a function to validate passwords"
  • Tests: Show exactly what valid and invalid passwords look like

Implementing Test-Driven Prompting

Generate tests first: "Write tests for a password validation function that:

  • Requires minimum 8 characters
  • Requires one uppercase letter
  • Requires one number
  • Rejects common passwords"

Then request implementation: "Now write the function that passes all these tests."

Benefits

  • Clarity — No ambiguity about requirements
  • Verification — Automatic correctness checking
  • Edge cases — Explicitly covered in tests
  • Refactoring safety — Tests catch regressions

When to Use

  • Complex business logic
  • Functions with many edge cases
  • Code that must be correct (payments, security)
  • When natural language specs feel ambiguous