Gherkin Test Cases with AI: Get Tables, JSON and CSV Output

A long paragraph from AI is hard to review and even harder to import. You can ask for exactly the layout you need. This lesson shows how to create Gherkin test cases with AI, and how to get clean tables, JSON and CSV for your daily QA work.

Advertisement

📖 What You’ll Learn

  • What structured output means
  • When to choose a table, Gherkin, JSON or CSV
  • How to ask for each format clearly
  • How to check the output before you use it

💡 What Is Structured Output?

Explain Like I’m 5: Structured Output

Imagine: Toys thrown in a pile are hard to find. Toys sorted into labelled boxes are easy to find.

What it really means: Structured output is an answer arranged in a fixed, predictable layout.

In testing: Every test case has the same fields in the same order.


🔍 Choosing the Right Format

Explain Like I’m 5: Choosing a Format

Imagine: You use a cup for juice, a plate for toast and a bowl for soup. Each one suits the food.

What it really means: Each format suits a different job.

In testing: Use a table for people to read, and JSON for programs to read.

FormatWhat it looks likeBest for
TableRows and columnsReading and reviewing test cases
GherkinGiven, When, Then sentencesBDD scenarios that a whole team can read
JSONNamed fields in curly bracketsTest data, API bodies and later automation
CSVPlain text with commasSpreadsheets and bulk import

Our examples use a shopping cart with one rule: quantity must be between 1 and 10.


🧩 Tables: Easy to Review

Name your columns in the exact order you want.

Return a Markdown table with these columns in this order:
ID, Scenario, Test Data, Expected Result, Type.
Do not add any text before or after the table.

🛠️ Gherkin: Scenarios Everyone Can Read

Explain Like I’m 5: Gherkin

Imagine: “Given I have a ball. When I kick it. Then it rolls.” It is a tiny story with a start, an action and a result.

What it really means: Gherkin writes behaviour in plain sentences using Given, When and Then.

In testing: Product owners, developers and testers can all read the same scenario.

Feature: Shopping cart quantity

  Scenario: Accept the highest allowed quantity
    Given a product is on the cart page
    When the shopper sets the quantity to 10
    Then the cart accepts the quantity

  Scenario: Reject a quantity above the limit
    Given a product is on the cart page
    When the shopper sets the quantity to 11
    Then an error message is shown
    And the quantity is not updated

Ask the AI to describe behaviour rather than clicks. “When the shopper sets the quantity” stays useful even if the button changes.


📌 JSON: Ready for Programs

JSON uses names and values. You will use it often for test data and API work.

{
  "feature": "Shopping cart quantity",
  "testData": [
    { "id": "QTY-001", "quantity": 1, "expected": "accepted", "type": "boundary" },
    { "id": "QTY-002", "quantity": 0, "expected": "rejected", "type": "boundary" },
    { "id": "QTY-003", "quantity": 10, "expected": "accepted", "type": "boundary" },
    { "id": "QTY-004", "quantity": 11, "expected": "rejected", "type": "boundary" }
  ]
}

Tell the AI to “return valid JSON only”. Then check it with a JSON validator or a code editor, because AI can still make syntax mistakes.


🔑 CSV: Ready for Spreadsheets and Import

CSV separates values with commas. If a value contains a comma, it must be wrapped in double quotes.

ID,Scenario,Quantity,Expected Result
QTY-001,Lowest allowed quantity,1,"Quantity accepted, cart updated"
QTY-002,Below the lowest quantity,0,"Error shown, cart unchanged"

Check your tool’s import rules first

Jira, Xray, Zephyr and TestRail do not all expect the same columns. Export a sample from your own tool or read its current import guide. Then give those exact column names to the AI.


💡 How to Check Structured Output

  1. Count the fields. Does every row have the same columns?
  2. Validate the syntax. Open the JSON in a validator or the CSV in a spreadsheet.
  3. Check the content. Correct formatting does not mean correct testing.
  4. Test a small import. Try one or two rows before you import many.

Tidy is not the same as true

A perfect table can still contain a wrong expected result. Review the content with the checklist from AI Hallucinations in Software Testing.


🧪 Try It Yourself

  1. Pick one rule from the Dummy E-commerce Website, or use the cart rule above.
  2. Run the prompt below, and read the Gherkin output.
  3. Ask the AI to convert the same scenarios into JSON.
  4. Paste the JSON into a validator and note any errors.
  5. Ask for CSV, then open it in a spreadsheet and check the columns.

📋 Ready-to-Copy Prompt

You are a QA engineer who writes clear BDD scenarios.

Feature: [feature name]
Rules:
- [rule 1]
- [rule 2]

Write Gherkin scenarios covering the positive, negative and boundary
cases for these rules.

Output rules:
- Use only Feature, Scenario, Given, When, Then and And.
- Describe behaviour, not button clicks.
- Use one behaviour per scenario.
- Use only the rules above. Do not invent any behaviour.
- Put anything unclear in a list called "Questions" after the scenarios.

⚠️ Common Mistakes

  • Not naming the columns you need
  • Assuming AI-generated JSON is always valid
  • Forgetting double quotes around CSV values that contain commas
  • Importing hundreds of rows before testing a small sample
  • Writing Gherkin as a long list of clicks

🎯 Key Takeaways

  • Structured output has a fixed layout that is easy to review and reuse.
  • Use tables for review, Gherkin for shared behaviour, JSON for programs and CSV for import.
  • Always validate the syntax, then review the content.
  • Match the exact import format of your own test tool.

🎤 Interview Questions

1. Why would you ask AI for structured output?

Model answer: Structured output is consistent, so it is faster to review and easier to use in other tools. I specify the exact columns or fields, then validate the format and review the testing content.

2. What is Gherkin, and what do Given, When and Then mean?

Model answer: Gherkin is a plain-language format for describing behaviour. Given sets the starting state, When describes the action or event, and Then describes the expected outcome. Tools such as Cucumber can link these steps to automation code.

3. How do you check AI-generated JSON?

Model answer: First I check that it parses in a validator or code editor. Then I check the field names and data types against the expected schema. Finally, I confirm that the values and expected results match the requirement.

4. How would you prepare AI-generated test cases for import into a test tool?

Model answer: I would get the tool’s required columns from its import guide or a sample export, and give those to the AI. After reviewing the content, I would import a small sample first to confirm the mapping.

5. What is the difference between a well-formatted and a correct test case?

Model answer: A well-formatted test case follows the agreed structure. A correct test case checks the right behaviour with the right expected result. AI can do the first perfectly and still fail the second, so I review both.


📝 Assignment

Take one feature and create its test set in all four formats: table, Gherkin, JSON and CSV. Validate the JSON and open the CSV in a spreadsheet. Write three lines explaining which format you would use for review, for your test tool and for automation.


❓ FAQ

Should I use Gherkin for every test?

No. Gherkin works best when a team uses it to agree on behaviour. A simple table or checklist may be better for routine checks.

Can I test JSON output with Playwright later?

Yes. JSON test data works very well with automated tests. You can learn the basics in the Playwright with TypeScript course.


🚀 Next Steps

Well done on finishing this lesson. Complete the assignment, then continue with How to Review AI-Generated Test Cases: A Beginner’s Guide.