When to use it

Use this prompt when you add tests to existing code that has none, when you want to check a new function before merging it, or when you are refactoring and need a safety net first. It is particularly useful for business logic with many rules and boundaries: pricing, date calculations, validation and parsing.

How to customize it

  • [LANGUAGE AND TEST FRAMEWORK] with versions ensures the generated syntax matches your setup.
  • [SPECIFICATION] is what makes the tests meaningful. Tests generated from the code alone tend to confirm whatever the code does today, bugs included. Describe what it should do.
  • [DEPENDENCIES TO MOCK] keeps tests fast and deterministic. Time, randomness, network and databases are the usual suspects.
  • [CODE UNDER TEST] should include any small helper functions it calls, so the model does not guess their behaviour.
  • [FRAMEWORK] in the last line keeps the model from introducing new test libraries.

Why it works

Listing the behaviours before writing code is a planning step that produces better coverage than asking for tests directly. The fixed categories (normal, boundary, invalid) act as a checklist of the cases developers most often forget.

Separating the specification from the code is the key technique. By comparing the two and reporting disagreements as questions, the model helps find bugs instead of writing tests that lock them in. Asking which tests should fail makes this explicit and gives you something to verify immediately. Constraints on structure, naming and determinism produce tests that a team can read and maintain. Our guide on using AI coding assistants effectively covers how to fit this into a workflow.

Tips and variations

  • Run the tests. Generated tests can contain wrong expected values, and a test is only useful once you have checked it.
  • If a test fails, decide whether the test or the code is wrong before changing either.
  • Add “Also suggest property-based tests” for functions with wide input ranges.
  • Ask for a coverage summary: which branches of the code are not exercised by any test.
  • For legacy code without a spec, first ask the model to describe the current behaviour, review that description, then generate tests from it.