ClaudeTestingLevel 2: Context

How Do You Turn Examples Into a Table-Driven Test?

Give Claude your input and expected-output pairs, and ask for one parameterized test where each example is a labeled row. Name your framework's form: test.each in Vitest, @pytest.mark.parametrize in pytest, or a table of cases run with t.Run in Go. Adding a case then means adding a row.

Prompt to copy
Here are examples of what [function, with path] should do. The expected values are mine, so treat them as the spec:

| label | input | expected |
| [short name] | [input] | [output] |
| [short name] | [input] | [output] |

Write one table-driven test in [Vitest test.each / pytest parametrize / Go t.Run], in [test file path], using the style already in that file. Use one assertion for every row, and show the label in the test name.

Run it and show the output. If a row fails, don't change its expected value or the code. List it and say whether you think the example or the code is wrong.

Then list any kinds of input my table doesn't cover, without adding rows for them.
  • Vitesttest.each, with titles filled from each row
  • pytest@pytest.mark.parametrize, reports the failing case
  • Goa table of cases run with t.Run
The table form in each framework, per the Vitest, pytest and Go docs linked on this page.

Try it now

  1. 1Collect 6 to 12 real examples from a ticket, a bug report, the docs or a spreadsheet.
  2. 2Paste the prompt with your table and framework.
  3. 3Read the failing rows first. A failure means your example and the code disagree, and only you can decide which is right.
  4. 4Add one row by hand and rerun. It worked if that one line is all you had to change.

Use it when

Formatters, validators, parsers, pricing rules and state transitions. Any time you'd otherwise copy a test and change two values.

Skip it when

Cases that need different setup or different assertions. A table with flags such as shouldMock or expectThrow hides the logic in conditionals, so write separate tests. Rule of thumb, not a vendor figure: skip it for two or three cases too, where a table adds more structure than it saves.

Why it works

A table puts every case in one place, so gaps are easy to see, and one test body runs them all. Frameworks can also tell you which row failed. pytest reports a failing case as test_eval[6*9-42], Vitest fills a title such as add($a, $b) -> $expected from each row, and Go shows the name you pass to t.Run. The Go wiki adds that t.Errorf lets the test continue, so you can see whether a function fails for all inputs or only some. Claude Code matches the frameworks and assertion patterns in your existing tests, so in a repo with tests you rarely need to spell out syntax. The real value of your examples is that you wrote the expected values, not the model.

Example

Say you have twelve slugify pairs, including Crème Brûlée to creme-brulee. In Vitest, the result should look like test.each([{ input: 'Crème Brûlée', want: 'creme-brulee' }, ...])('slugify($input) -> $want', ({ input, want }) => { expect(slugify(input)).toBe(want) }). Then read the table and the list of gaps Claude gives you. You may notice that nothing says what happens to an empty string or a leading number. That decision is yours, and it's easier to see in a table than across twelve separate test functions.

Going further

Two framework details are easy to miss. Vitest's test.each spreads array rows into separate arguments, while test.for doesn't, and it passes TestContext as the second argument, so ask for test.for when a test needs that context. pytest escapes non-ASCII characters in parametrized strings by default, so short ASCII labels read better than raw inputs in its report. Rule of thumb, not a vendor figure: keep a long table in a data file such as slugify.cases.json, which your docs can read too, so the published examples and the tested ones are the same list.

Common mistakes

  • Letting Claude fill in the expected values. If it works them out by reading the code, the table only confirms the code.
  • Leaving rows unlabeled, so a failure gives you a number and you have to count down the table.
  • Letting it fix a failing row by changing the expected value. Say in the prompt that your values are the spec.
  • Cramming cases with different setup into one table, which produces a test full of conditionals nobody wants to read.

Sources

Checked 6 November 2026. Products change often, so confirm details with the vendor.

Related

Beyond the course