Library › Book 2 › The Anatomy of a Test-Generation Prompt
The Anatomy of a Test-Generation Prompt
1.1Why Prompt Structure MattersA poor prompt produces poor tests. This is not a vague principle -- it is a direct, measurable relationship. The difference between "write…1.2The Five Elements of an Effective Test-Generation PromptEvery high-quality test prompt contains five elements. Missing any one of them degrades output quality significantly. These elements form…1.3Element 1: ContextContext tells the LLM what kind of system it is dealing with. Without it, the LLM makes generic assumptions. With it, the LLM tailors its…1.4Element 2: ArtifactThe artifact is the primary source of truth for test generation. It is the document that defines what the code should do. Without it, the…1.5Element 3: ConstraintsConstraints prevent the LLM from generating tests in the wrong framework, language, or style. They also enforce team conventions that make…1.6Element 4: Coverage GoalsWithout explicit coverage goals, the LLM defaults to happy-path tests. This is perhaps the most critical element to get right, because it…1.7Element 5: Output FormatControlling the output format makes the generated code immediately usable rather than requiring reformatting.1.8Putting It All Together: A Complete PromptHere is a complete prompt that includes all five elements:1.9The Prompt Iteration LoopPrompt engineering is not one-shot. The first prompt rarely produces perfect output. Use this iteration loop:1.10Q&ASelf-Assessment QuizTest your understanding of Chapter 1:1.11Key Takeaways- Treat your prompt as a test specification document, not a casual request - The five elements (Context, Artifact, Constraints, Coverage…1.12ExercisesExercise 1.1 (Beginner): Take a feature you recently tested at work. Write a test-generation prompt that includes all five elements. Do not…1.13Career Translation- Engineered structured test-generation prompts using the five-element framework (Context, Artifact, Constraints, Coverage Goals, Output…1.14Q&AInterview Depth CheckPrompt: You are asked to generate tests for a checkout API at a new company. You have never seen the codebase. What information do you…