Example result: Write the tests first for a new feature
AI-generated example · Test-Driven
Updated

Write the tests first for a new feature

This prompt writes the tests before the code. You list the rules of the feature and it writes one failing test per rule, plus edge cases, and flags any rule that was unclear so you can confirm it first.

text-code-test-first-feature — claude — 87 words

Act as a developer who practises test-driven development.

Feature: [WHAT IT SHOULD DO, e.g. apply a coupon code to a cart]
Rules:
[LIST THE RULES, e.g. one coupon per cart, expired codes rejected]
Language and test framework: [e.g. TypeScript with Vitest]

Write the failing tests first, one per rule, plus edge cases. Use names that read like sentences. Do not write the implementation yet. After the tests, list any rule that was unclear and the assumption you made, so I can confirm before we write the code.

Check the tests match what you want before asking for the code. Then ask: "now write the simplest code that makes these pass".

Get a better result

What you will get

A set of failing tests, one for each rule plus edge cases, with names that read like sentences and no implementation yet, followed by a list of unclear rules and the assumption made for each.

Common mistake

The AI writes the implementation along with the tests.

Fix: Keep “do not write the implementation yet”, and ask for only the test file. Run it and check that every test fails first.

Also watch for
  • Tests check how the code works inside, not what it does. Ask for tests that call only the public function and check its result, so you can change the inner code later without rewriting them.
  • A vague rule becomes a silent guess. Read the assumptions list before you go on. Correct any wrong one in your reply and ask for the affected tests to be redone.
Try a variation: add a property of the rules

Ask for “one more test that states a rule that must always hold, such as the total never going below zero”. It catches cases you did not list.

Still not right? Reply with this

Update the tests using my answers to the assumptions. Show which test changed and why, then stop before any implementation.

Guides that use this prompt

More chat & writing prompts

See all 465 →