Skip to content
QAcademy — QAcademy

Prompt engineering ·

Few-Shot Prompting for Gherkin Test Scenarios: Get Output in Your Team's Format

By The QAcademy team

Illustration for the article "Few-Shot Prompting for Gherkin Test Scenarios"

You ask an LLM to "write Gherkin scenarios for this user story". Ten seconds later you have a feature file, and you cannot use it. The steps describe clicks ("When I click the Submit button") while your team writes business-level steps. There are no tags, no Scenario Outline for the boundary values, and half the steps match none of your existing step definitions. You rewrite most of it, and the time saved is gone.

The model knows Gherkin perfectly well. What it does not know is your Gherkin: your step vocabulary, your tagging scheme, how finely you split scenarios. The most direct way to teach it, inside the prompt, is few-shot prompting: show it a few examples of the expected result and let it follow the pattern. This article covers how to choose those examples, how many to use, and where it goes wrong.

Zero-shot vs one-shot vs few-shot for Gherkin

The ISTQB® CT-GenAI syllabus (section 2.1.2) separates three cases:

Technique What the prompt contains Typical effect on Gherkin output
Zero-shot prompting Instructions only, no example Valid syntax, but the model's default style: generic steps, random granularity
One-shot prompting One example The format is copied, but the model sticks closely to that single example, quirks included
Few-shot prompting Several examples The model generalizes the shared pattern: step wording, tags, how scenarios are split

With zero-shot, the model relies only on what it learned during training, and "the average Gherkin on the internet" is not what lives in your repository. Few-shot prompting does not retrain or modify the model. It gives it, for one request, a concrete reference to imitate.

The syllabus is explicit about where this fits. In the table in section 2.2.5, few-shot prompting is the recommended technique for repetitive tasks or tasks with a specific, constrained output format, and Gherkin-style test cases are named as a typical case. Writing scenarios for the tenth user story of the sprint is exactly that.

Why an example beats a page of style rules

You can try to describe everything in instructions: "use declarative steps, add a priority tag, use Given/When/Then, one rule per scenario…". It works up to a point, but style rules leave room for interpretation. An example does not. It shows the length of a step, the level of abstraction, where tags go and how scenarios are named.

Research on in-context learning points the same way. On classification tasks, Min et al. (2022) found that whether the answers in the demonstrations were correct mattered less than expected, while the format, the space of possible answers and the kind of inputs shown mattered more. The practical lesson for testers: examples are very good at imposing form, and the model will not flag an example that is wrong. A sloppy example produces sloppy scenarios, nicely formatted.

How to choose few-shot examples for Gherkin

This is where most of the result is decided. The Prompt Report (Schulhoff et al., 2024), which the syllabus cites, lists the number of examples, their order, their format and their similarity to the target among the design decisions of a few-shot prompt. For Gherkin, that translates into four criteria.

1. Real examples that already passed review

Take your examples from .feature files that were reviewed and run in CI. They reflect the conventions your team actually follows, not the ones it thinks it follows. Replace any real data with fictitious data before pasting it into a prompt, especially if the tool is not approved by your organization for that kind of content.

2. Complete examples: user story, test conditions, scenarios

Hands-on objective HO-2.2.2b in the syllabus describes each example as a set: a user story, its test conditions, then the expected Given-When-Then test cases. It is a good habit. Showing the intermediate test conditions teaches the model how to get from a story to scenarios, not just what the final file looks like. It also lets you review the generated test conditions before reading a single line of Gherkin.

3. Varied examples

If both your examples are happy paths, you will mostly get happy paths. Vary them on purpose:

  • one positive scenario and one negative scenario;
  • one Scenario Outline with an Examples table for boundary values;
  • one example using Background if your team uses it.

4. Close to the domain, but not the same feature

An example on the same feature as the target story pushes the model to copy steps and values instead of reasoning. Use neighboring features from the same application, with similar kinds of rules.

How many examples?

The syllabus does not set a number; it talks about selecting "n examples". Anthropic's prompting documentation recommends 3 to 5 relevant, diverse examples for best results. A Gherkin example is long (story + conditions + scenarios), so in practice:

  • 2 examples are often enough when your format is simple and stable;
  • 3 examples when you need to show a positive case, a negative case and a Scenario Outline;
  • beyond that, the prompt grows, costs more and dilutes the instructions, usually for little gain.

Decide by testing. If the output drifts from your format on one specific point, add an example that shows that point, rather than one more example picked at random.

A complete few-shot prompt for Gherkin test scenarios

Fictional context: a web portal where policyholders file car insurance claims. The team writes declarative steps and shares a step catalog. Here is the prompt (examples shortened for readability), built on the six components described in our article on the structured prompt.

ROLE
You are a test automation engineer writing Gherkin scenarios for a BDD team.

CONTEXT
Application: web portal for filing car insurance claims (individual policyholders).
Feature files are executed with Cucumber.

INSTRUCTION
For the target user story, first list the test conditions, then write the
Gherkin scenarios, following the style of the examples exactly.

EXAMPLES
<example id="1">
User story: As a policyholder, I want to attach up to 5 photos to my claim
so that I can support my request.
Test conditions:
TC1 - Attaching 1 to 5 photos is accepted
TC2 - A 6th photo is rejected with a message
TC3 - A disallowed file type is rejected
Gherkin:
@claims @attachments @P1
Scenario: Attach the maximum number of photos
  Given a signed-in policyholder with a draft claim
  When they attach 5 JPG photos
  Then the claim has 5 attachments
@claims @attachments @P2
Scenario: Reject a photo above the limit
  Given a signed-in policyholder with a draft claim containing 5 photos
  When they attach one more photo
  Then the message "You have reached the maximum number of photos" is shown
  And the claim still has 5 attachments
</example>

<example id="2">
User story: As a policyholder, I want to enter the date of the incident
so that my claim is handled under the right policy terms.
Test conditions:
TC1 - A date within the coverage period is accepted
TC2 - A future date is rejected
TC3 - A date before the policy start date is rejected
Gherkin:
@claims @incident-date @P1
Scenario Outline: Validate the incident date
  Given a policyholder whose policy started on "2026-03-01"
  When they enter the incident date "<date>"
  Then the date is "<result>"
  Examples:
    | date       | result   |
    | 2026-03-01 | accepted |
    | 2026-02-28 | rejected |
    | tomorrow   | rejected |
</example>

CONSTRAINTS
- Declarative steps (what the policyholder does), never UI actions
  (no "click", "type into the field").
- Reuse steps from the catalog provided whenever possible; mark any new step
  with the comment # NEW STEP.
- One scenario per test condition. Tags: domain, feature, priority.
- Do not invent rules that are not in the user story: list your questions at the end.

INPUT DATA
Step catalog: [catalog excerpt]
Target user story: As a policyholder, I want to change the refund bank account
(IBAN) on my claim while it is still open, so that I get paid to the right account.

OUTPUT FORMAT
1. Test conditions (TCn - label)
2. Gherkin block
3. Open questions

What the output shows, and what you still have to check

Excerpt of the response (commented):

TC1 - Changing the IBAN on an open claim is accepted
TC2 - An IBAN with an invalid format is rejected
TC3 - Changing the IBAN on a closed claim is rejected

@claims @iban @P1
Scenario: Change the IBAN on an open claim
  Given a signed-in policyholder with a claim "under review"
  When they replace the refund IBAN with a valid IBAN
  Then the new IBAN is saved on the claim
...
Open questions:
- Is a confirmation (SMS code, email) required to change the IBAN?
- Are non-SEPA IBANs accepted?

What worked: tags, granularity, declarative style and the "one scenario per test condition" rule were all carried over. What few-shot prompting does not do for you:

  • check coverage (see our method to generate test cases with AI): there is no condition for a claim "waiting for documents" (is it open or not?). The format is right; the coverage is not necessarily;
  • review every step marked # NEW STEP before it reaches the automation engineers;
  • take the open questions to the Product Owner instead of letting the model decide.

For the test design itself (equivalence partitions, boundary values), the classic techniques still apply: see our guide to test design techniques.

Common few-shot prompting pitfalls in Gherkin

The model copies content, not just form. If your examples mention "5 photos" and "2026-03-01", do not be surprised to see those values reappear in new scenarios. Use example data that is clearly different from the target, and review generated values.

Example order matters. Zhao et al. (2021) showed that the choice of examples, their format and even their order can make results vary widely, with models tending to favor what appears near the end of the prompt. In practice: put last the example whose style you most want to dominate, and try a different order if the output surprises you.

Examples that contradict the instructions. If the instructions say "declarative steps" but one example contains "When I click Submit", the example often wins. Review your examples against your own constraints.

Examples that all look alike. Three happy-path examples are an invitation to write only happy paths.

Confusing conformance with quality. A well-formatted scenario can test the wrong thing. Few-shot prompting makes output more consistent; it does not replace test analysis or review.

Examples that go stale. When your step catalog changes, update the examples. Treat them like testware: versioned, reviewed, with an owner.

Few-shot or another technique?

Few-shot prompting solves a format problem. If the hard part is analyzing a vague user story, prompt chaining (steps verified by a human) fits better. If you do not know how to phrase the first prompt, meta prompting helps you build it with the model. And the syllabus points out that the techniques combine well.

Key takeaways for the CT-GenAI exam

  • GenAI-2.1.2 (K2): differentiate zero-shot, one-shot and few-shot prompting, and tell them apart from prompt chaining and meta prompting. HO-2.1.2b asks you to identify the technique used in a given prompt: look for whether examples are present, and how many.
  • GenAI-2.2.2 (K3) and HO-2.2.2b: few-shot prompting applied to generating Gherkin-style test conditions and test cases from user stories, refining the prompt or the examples when results are inaccurate.
  • GenAI-2.2.5 (K3): choose the technique that fits the task. Repetitive or constrained output format (Gherkin, keyword-driven tests, reports in a fixed format): few-shot.
  • GenAI-2.3.2 (K2): evaluating and iteratively refining prompts, which includes adjusting the examples.

Also remember the nuance: few-shot prompting puts examples in the prompt; it does not retrain the model. To see how these objectives fit into the whole program, read the CT-GenAI certification guide and the exam guide.

FAQ

Does few-shot prompting work with Gherkin in other spoken languages?

Yes. Cucumber supports many languages through a # language: header on the first line of the feature file. Provide that header and examples in the target language: the model follows the language of the examples far more reliably than a one-line instruction.

Do the examples have to be perfect?

They have to be correct and match your conventions, because their flaws will be reproduced. They do not have to be long: two or three scenarios per example are enough to convey the style.

Can I reuse the same prompt for every user story?

Yes, that is the point: only the target story changes. Keep the prompt in a shared repository, version it, and revise the examples when your conventions change.

Where to go next

Few-shot prompting is one of the techniques the CT-GenAI syllabus expects you to recognize, choose and apply. If you want to know where you stand on these topics before you start, the free diagnostic takes 5 minutes and 10 questions, with no commitment.

Sources

  • ISTQB®, Certified Tester Specialist Level – Testing with Generative AI (CT-GenAI), syllabus v1.1, sections 2.1.2, 2.2.2 and 2.2.5: istqb.org
  • Anthropic, Prompting best practices – Use examples effectively: platform.claude.com
  • Cucumber, Gherkin Reference (keywords, Scenario Outline, localization): cucumber.io
  • T. Z. Zhao et al., Calibrate Before Use: Improving Few-Shot Performance of Language Models, ICML 2021: proceedings.mlr.press
  • S. Min et al., Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?, EMNLP 2022: aclanthology.org
  • S. Schulhoff et al., The Prompt Report: A Systematic Survey of Prompting Techniques, 2024: arxiv.org

Take the next step

You have just read. Now see where you stand: 10 questions, your score chapter by chapter, free and without signing up.

Take the free test (5 min)

Already decided to practise?

One-off payment, no subscription, refunded within 14 days if it is not for you.

Get the new articles by email

At most one email a week: the new QAcademy articles on software testing and quality. Free, unsubscribe in one click.

5 minutes · free · no sign-up

Before you go: where do you stand?

10 questions in the exam format, about 5 minutes: your score by chapter and your gap to the pass mark.

5 minutes · free · no sign-up