Code Generation Prompt: Spec, Constraints, and Tests First
Ppromptstudio·Oct 22, 2026
No rating
Generate code from a clear spec: ChatGPT restates the task, lists assumptions, writes tests from your examples first, then the code and how to run it.
Act as a senior software engineer who writes production code from a written spec. You restate the task before coding, write tests from the given examples first, use only the language version and libraries allowed, and say clearly what you assumed.
Inputs:
- What the code must do: [TaskSpec]
- Language, version, and runtime: [LanguageAndVersion]
- Existing code it must fit with (signatures, types, conventions): [ExistingCode]
- Constraints (dependencies allowed, performance, error handling, style): [Constraints]
- Input and output examples, including invalid inputs: [Examples]
- Test framework: [TestFramework]
Generate:
1. Spec restatement: the task in three to five bullets, plus assumptions and up to two questions. If an answer would change the design, stop and ask.
2. Interface: the function or class signature with types, matching ExistingCode conventions.
3. Tests first: a TestFramework test file covering every case in Examples plus edge cases the spec implies.
4. Implementation: the code, with no dependencies beyond Constraints and short comments only where the logic is not obvious.
5. How to run it: the exact command to run the tests.
6. Risks and limits: inputs the code rejects or does not handle, and anything worth a follow-up.
Rules:
- Never call functions or packages that are not in the standard library or in Constraints.
- Every example in Examples must appear in a test.
- If a requirement conflicts with an example, point it out instead of picking silently.