[KongchangAI]
· 3 min read· 1,534 words

How to Prompt LLMs to Generate Genuinely Useful Test Cases

How to Prompt LLMs to Generate Genuinely Useful Test Cases

Master four-element prompts and five templates to make LLMs generate genuinely useful test cases.

This article argues that poor test case output from large language models usually stems from the prompt, not the model. An effective prompt needs four elements—role, real context, specific task verbs, and explicit format—with real requirement context being most critical. It provides five reusable templates: functional cases from user stories, boundary and negative scenarios, security-oriented ideas, Gherkin BDD scenarios, and model self-review. It also flags common AI pitfalls and offers a six-item review checklist plus an automation handoff workflow.

Ask a large language model to "write test cases for the login page," and you'll get output that looks polished but actually tests almost nothing. The content reads smoothly and the format is neat, but it's all generic scenarios: valid login, wrong password, empty fields. Any junior tester could list those in two minutes.

The problem isn't the model—it's the prompt. An LLM can only work with the information you give it, and "write test cases for login" tells it nothing about your product, your rules, or what counts as a failure.

According to a practical guide shared on the CloudQA blog, an effective test case generation prompt needs four core elements, along with five prompt templates you can apply directly.

The Four Components of an Effective Prompt

Regardless of which model you use, prompts that produce usable test cases all contain the same four things:

  1. Role assignment: "Act as a senior QA engineer." This sets the professional level and terminology of the response.
  2. Real context: Paste in the actual user story, acceptance criteria, API spec, or code snippet. This matters more than any other element on the list. When the model reads real requirements, it can spot real gaps; when it reads "a login form," it can only guess.
  3. Specific task: Use direct verbs—"generate," "list," "identify." Avoid vague phrasing like "think about" or "consider."
  4. Output format: Ask for a table (with specified columns), Gherkin, or JSON. If you plan to paste the results into a test management tool, say so directly.

If you can only change one thing, change the context.

Illustration of prompt-based test case generation

Five Prompt Templates You Can Apply Directly

Template 1: Generate Functional Test Cases from a User Story

The most common scenario—you have a user story and acceptance criteria, and you need a first round of coverage. In the prompt, ask the model to "include positive, negative, and boundary cases," and to "if any acceptance criterion is ambiguous, list your interpretation in a separate 'Assumptions' section rather than silently choosing one interpretation."

The "Assumptions" instruction is the single most valuable line in the whole prompt. It makes the model surface its reasoning about requirement gaps instead of papering over them—and those gaps are often where the real bugs live.

For an "add to cart" feature, for instance, a good output will include scenarios the acceptance criteria never mention: the expected behavior when a user clicks the button on an out-of-stock item, whether quantity stays consistent under rapid repeated clicks, whether duplicate line items get created. These are exactly where the model adds value—and where you should focus your review.

Template 2: Boundary Cases and Negative Scenarios

Models are great at brainstorming edge inputs people tend to forget. Ask the model to "group by category"—input validation, session behavior, rate limiting, accessibility—and to state the system's expected behavior in each scenario.

Unstructured lists drift toward the obvious cases. A grouping requirement forces the model to cover more dimensions.

Template 3: Security-Oriented Test Ideas

For testing your own application, the model can quickly generate a checklist of common vulnerability checks, such as SQL injection and cross-site scripting. For each case, provide the input, the target field, and the expected security behavior.

To be clear: these outputs are only a starting checklist. They don't replace a professional security assessment or dedicated security tooling. And you should only run these tests against systems you own or are explicitly authorized to test.

SQL injection is when an attacker inserts malicious SQL fragments into input fields, causing the application to treat user input as database commands—thereby reading, tampering with, or deleting data. Typical test ideas include trying payloads like ' OR '1'='1 or '; DROP TABLE-- in usernames, search boxes, and ID parameters, then observing whether the system returns anomalous data or error messages. Cross-site scripting (XSS) involves injecting malicious JavaScript into a page so that it executes in other users' browsers when they view it, potentially stealing cookies or session tokens. When testing, you typically submit payloads like <script>alert(1)</script> to reflection points such as comments, nicknames, and URL parameters, then check whether the output is properly escaped. Beyond these, complete security testing should also cover authentication bypass, broken access control, CSRF, and sensitive information disclosure—all of which go beyond mere test case generation and require dedicated tools like SAST and DAST plus manual penetration testing.

Template 4: BDD Scenarios in Gherkin Format

If your team uses Behavior-Driven Development (BDD) specs, you can have the model generate a first draft of the feature file. Ask it to "use Scenario Outline with Examples to cover input variations," and include both positive and negative paths.

BDD (Behavior-Driven Development) and Gherkin are a collaborative methodology for describing software behavior in natural language. The core idea is to get business, development, and testing to align on requirements using the same language. Gherkin is the most commonly used syntax format for BDD, writing test cases as structured near-English text using keywords like Feature, Scenario, Given, When, and Then: Given describes preconditions, When describes the triggering action, and Then describes a verifiable expected result. Scenario Outline with Examples tables lets a single scenario run repeatedly across multiple sets of inputs—ideal for covering boundary values and input combinations. These files are usually saved with a .feature extension and can be parsed directly by frameworks like Cucumber, SpecFlow, and Behave to drive automated tests. The value of having an LLM generate a Gherkin first draft is quickly turning user stories into structured scenarios—but a human still needs to confirm that the expected results in Then matches the actual system behavior, or you risk cementing behaviors the model invented.

Template 5: Have the Model Review Itself

This template is underused but highly valuable. After generating a test suite, use a second prompt to review it: Which requirements have no test coverage? Which cases are duplicates or near-duplicates? Which cases assume behavior not stated in the requirements? What are the five most likely missed high-risk scenarios?

The third question is the most critical—it exposes cases where the model "invented behavior," which is the most common failure mode in AI-generated tests.

Common Pitfalls of AI-Generated Test Cases

LLMs are good first-draft engines, but they are not oracles.

  • They don't know your business: The model doesn't know that refunds over a certain amount require manager approval, or that a particular customer tier gets special pricing. It only knows what you put in the prompt.
  • They fabricate plausible-sounding behavior: If the requirements don't mention it, the model may fill in an answer that sounds right but is wrong for your product. Tests based on fabricated behavior will fail for the wrong reasons—or worse, pass for the wrong reasons.
  • They produce quantity, not priority: Thirty test cases aren't better than ten if you can't tell which five matter most.
  • Generated automation code is just a draft: If you ask for Playwright or WebdriverIO scripts, you need to verify every selector and wait strategy against the real application. Generated selectors are often guesses.
  • Data privacy is your responsibility: Don't paste proprietary code, customer data, or confidential requirements into public model interfaces. Use enterprise solutions that meet your organization's data protection requirements.

Review Checklist and Automation Handoff

Before AI-generated test cases enter your suite, check each one: Can it be traced back to a real requirement? Is the expected result explicitly stated or merely implied? Does it assume unconfirmed behavior? Does it duplicate an existing case? If it fails, can you tell why? Is it worth automating, or is it better suited as an exploratory check?

Cases that fail this checklist should be rewritten or discarded.

Once they pass review, a typical workflow is: generate cases → review → pick the ones worth automating → convert to scripts. You can have the model generate framework-specific code, but always add this line: "Add a comment above any selector you are unsure about." That instruction gives you a checklist of items to verify manually.

Key Takeaways

Better prompts come from better inputs: real requirements, explicit formats, and asking the model to state its assumptions. The model handles the fast drafting; your job is to judge what's worth keeping.

One-line Summary

Master four-element prompts and five templates to make LLMs generate genuinely useful test cases.

Paragraph Summary

This article points out that when using large language models to generate test cases, poor output quality often stems from the prompt itself rather than the model's capability. An effective prompt must contain four elements: role assignment, real context, specific task verbs, and explicit output format—with pasting in real requirement context being the most critical. The article offers five directly applicable templates: generating functional cases from user stories, boundary and negative scenarios, security-oriented ideas, Gherkin-format BDD scenarios, and having the model self-review. It also flags common pitfalls of AI-generated cases, including not knowing business rules, fabricating plausible but wrong behavior, favoring quantity over priority, needing line-by-line verification of generated code, and data privacy responsibility. Finally, it provides a six-item review checklist before cases enter the test suite and a workflow for handing off to automation.

#LLM test case generation#AI test case prompts#prompt engineering for QA#AI-assisted testing#Gherkin BDD scenarios
Share:

Related articles