Skip to content
mentoor

Topic 2 of 5 · 4 min read

Drafting test cases with an agent

Goal: Get an agent to propose test cases from a spec and add the missing edge and failure cases before any test code exists.

Ask the agent for a plain list of test cases first, review and extend that list, and only then ask for test code. The list is cheap to read. Test code is not.

Check yourself · 1 of 3

What should you ask an agent for first when you want tests for a new function?

Choose one answer

Why it matters

An agent is good at turning a spec into tests quickly. It is weak at doubt. Left alone, it writes the happy path, a couple of obvious errors, and stops. It rarely thinks of the empty list, the duplicate request, the time zone or the second call that arrives while the first is still running. Kent Beck's description of TDD starts with a test list for this reason: you decide what to test before you write anything.

How to do it

  1. Give the spec and the interface. Paste the acceptance criteria, the function signature and any rules about errors.
  2. Ask for cases, not code. "List the test cases for this function as one line each: the input, and the expected result or error. Do not write code yet."
  3. Review the list against four families. Normal cases, boundaries (zero, one, the limit, one over the limit), invalid input (null, wrong type, too long), and failure of a dependency (the database is down, the call times out).
  4. Add what is missing yourself. Then ask the agent: "What cases would a careful reviewer say are missing?" Treat the answer as suggestions, not truth.
  5. Remove duplicates and low value cases. Ten tests on the same path are not ten times safer.
  6. Ask for the code one group at a time. Review each batch, run it, see it fail.

Before and after

Weak prompt: "Write tests for parseQuantity."

Better:

Spec: parseQuantity(text) turns "3", "3.5" or " 3 " into a number.
It rejects empty text, negative numbers, and values above 999.
Return a number, or throw InvalidQuantity.

List test cases, one line each: input -> expected. No code yet.
Include boundaries and bad input.

The agent returns a list. You notice it has no case for "1e3" or "3,5". You add both, and you decide what each should do before any test is written.

What goes wrong

  • A long list that all tests one path. Count the families, not the lines.
  • Cases invented without the spec. The agent may guess a rule, such as rounding, that the spec never stated. Each case needs a source in the spec or a decision from you.
  • Skipping the list. Reading 40 generated tests is slow, so people stop reading. A 12 line list gets read.

Examples

Boundaries the agent skipped

The spec says orders over 500 need approval. The agent's list has "600 needs approval" and "100 does not". You add "exactly 500" and "500.01", because the rule says "over" and the boundary is where code goes wrong.

The dependency case

For a function that saves a user, the first list has five happy cases. You ask: "What happens if the database call throws?" The agent adds two cases. You decide the function should rethrow a typed error, and you write that into the list.

Key points

  • Ask for a list of cases, one line each, before any test code is written.
  • Review the list against normal, boundary, invalid input and dependency failure cases.
  • Add the missing cases yourself, then ask the agent what a careful reviewer would say is missing.
  • Every case needs a source in the spec or a decision from you, not a guess by the agent.
  • Generate and review the code in small batches, running each batch to see it fail.

Common mistakes

  • Asking for test code straight away and then skimming 40 tests.
  • Accepting the agent's cases without checking each against the spec.
  • Counting lines in the list instead of the kinds of case it covers.
  • Letting the agent decide what an error case should return, without a decision from the owner of the behaviour.

Check yourself · 2 of 3

A function rejects quantities over 999. Which pair of cases tests the boundary best?

Choose one answer

Check yourself · 3 of 3

The agent's case list has only successful inputs. What is the best next step?

Choose one answer

Practice items, not counted towards your mastery.

Start here