Skip to content
Back to AI Coding

Test Generation Prompts

Generate useful tests: cover happy path, edge cases, and failure modes — not just the obvious.

Prompt EngineeringIntermediate

Why

GitHub's 2024 survey found 92% of US respondents use AI coding tools to generate test cases, and 73% of open source respondents use AI tools for coding or documentation. Default AI tests cover only the happy path and look green but prove little.

Asking for edge cases, failure modes, and boundary values produces tests that actually catch regressions.

When

Use when generating tests for any non-trivial function. Skip for trivial getters.

How

1. Give the function + its types + an example call. 2. Ask: "list edge cases and failure modes first". 3. Then: "write a test per case: happy path, each edge, each failure". 4. Require assertions on the return value, not just "doesn't throw". 5. Run coverage; ask for tests on uncovered branches.

Example

fn: parseAge(input: string): number | null Edge cases: "", "abc", "-5", "999", " 42 ", "0", "2.5" Failure modes: non-numeric → null, negative → null, >150 → null Tests assert exact return per case, not just "no throw".

Common Problems

  • ⚠Error: Model returns "I cannot help with that" → rephrase to remove restricted phrasing; narrow scope and provide explicit allowed examples.
  • ⚠JSON parse error on model output → wrap with a fenced ```json block and add "respond with valid JSON only" to the system prompt.
  • ⚠Error: Token limit exceeded (400 Bad Request) → split the prompt, summarize prior context, or switch to a larger context model.

Best Practices

  • ✓Ask for edge cases before tests so the model enumerates them.
  • ✓Assert exact return values, not just absence of throws.
  • ✓Measure the outcome with a concrete metric (time saved, defect rate, test coverage) so the practice can be justified and improved.

FAQ

What is Test Generation Prompts?

Generate useful tests: cover happy path, edge cases, and failure modes — not just the obvious. It helps developers quickly grasp the core value and use cases of Test Generation Prompts.

How do I get started with Test Generation Prompts?

To get started with Test Generation Prompts, focus on these points first: Give the function + its types + an example call.

What are common mistakes with Test Generation Prompts?

Common mistakes when using Test Generation Prompts include: Error: Model returns "I cannot help with that" → rephrase to remove restricted phrasing; narrow scope and provide explicit allowed examples.; JSON parse error on model output → wrap with a fenced ```json block and add "respond with valid JSON only" to the system prompt.; Error: Token limit exceeded (400 Bad Request) → split the prompt, summarize prior context, or switch to a larger context model..

What are the best practices for Test Generation Prompts?

Best practices for Test Generation Prompts include: Ask for edge cases before tests so the model enumerates them.; Assert exact return values, not just absence of throws.; Measure the outcome with a concrete metric (time saved, defect rate, test coverage) so the practice can be justified and improved..

How do I learn more about Test Generation Prompts?

Start with the official docs, then search real-world examples on GitHub. Validate patterns in a small scratch repo before applying them to production code.