Build a Test Set

Reading real outputs tells you how an LLM app fails. A test set turns that knowledge into something you can run again and again: a fixed list of inputs, each with a written statement of what a good answer must do. The heart of each case is that statement: it says what a good answer does, not the exact words it uses.

One input and what a good answer must do

A test case is one input together with a statement of what a good output must do. The input is what the app receives, such as a customer's message. The statement is the part that makes it a test: without it, you can send the input and read the reply, but you have nothing to judge the reply against.

Take a help assistant for an online shop. The shop accepts returns within 30 days of delivery. The assistant can explain that policy but cannot issue refunds itself, and when a customer disputes a charge it should hand the conversation to a person. One test case for it, stored as a small record, could look like this:

One test case
{
  "id": "return-window-03",
  "failure_mode": "wrong-policy-fact",
  "input": "My kettle arrived 3 weeks ago and it leaks. Can I still return it?",
  "must_include": [
    "returns are accepted within 30 days of delivery",
    "the order number is needed to start a return"
  ],
  "must_not_say": [
    "a refund has already been issued",
    "the item cannot be returned"
  ],
  "action": "answer"
}

The record holds the input and three kinds of expectation: facts the answer must contain, things it must never say, and the action it must take (here, to answer rather than decline or hand off). The id lets you name the case in a report, and the failure_mode tag says which kind of failure the case is there to catch; both come up again below.

The statement has to be precise. A useful test is to imagine two people who know the shop reading the same reply: if they would both reach the same pass or fail verdict from the case alone, the case is clear enough. "Gives a helpful answer about returns" fails that test, because each reader fills in their own idea of helpful. "States the 30-day window" passes it.

Describe the behaviour, not the wording

An ordinary automated test often compares the result with one expected value. That works when there is exactly one correct answer, such as a number or a date. A language model can phrase a correct reply in many ways, and a reply can be fluent and still wrong. Comparing a reply with one reference text therefore misjudges it in both directions: a correct reply fails because its wording differs, and a wrong reply can look close to the reference.

Suppose the reference answer for the case above is "Yes, you can return it within 30 days of delivery. Please have your order number ready." Here is how three real-looking replies fare against that text and against the case's expected behaviour:

ReplySame text as the referenceMeets the expected behaviour
Three weeks is inside our 30-day window, so yes. Send me your order number and I can start the return.noyes
Yes, returns are accepted within 14 days, so please have your order number ready.nono: wrong fact
So sorry about that! I have refunded your order, you will see the money in 3 days.nono: says a refund was issued

The text comparison fails all three, so it cannot tell the good reply from the two bad ones. The behaviour statement passes the first and fails the other two, each for a reason you can name. That is why a case describes a good output by what it must do:

  • Facts it must contain: the 30-day window, the need for an order number.
  • Things it must never say: that a refund was issued, a made-up policy, advice outside the app's job.
  • The action it must take: answer, decline, or hand the conversation to a person. When the app returns structured output, the action is often a field whose value can be read directly.

A reference answer still has a use. Writing one known good reply that meets every expectation of the case proves the case can be passed at all, and catches a case whose expectations contradict each other. It is a check on the case, not the text a reply has to match.

True or false?

A reply that uses none of the reference answer's wording can still pass a well-written test case.

True. The case judges what the reply does: the facts it states, what it avoids and the action it takes. Different wording that does all of that passes.

Matching one reference answer word for word is the most reliable way to test an LLM app.

False. Exact matching only fits answers with one objectively correct form. For free text it fails correct replies worded differently and says nothing about why a reply is wrong.

"Answers the question politely and helpfully" is a precise enough expectation for a test case.

False. Two readers would disagree on what counts as helpful. A case should name concrete facts, forbidden statements and an action, so anyone reaches the same verdict.

Where the cases come from

A test set is only as good as the inputs in it. Invented questions tend to be the easy, tidy ones the app already handles, so the cases should come from three places that reflect how the app is really used and how it really fails.

  • Failures found in review outputs judged fail
  • Common requests what users ask most
  • Edge cases out of scope, odd input
  • Test set input + expectations
  • Failures found in review to Test set
  • Common requests to Test set
  • Edge cases to Test set

Failures found in review. Every output that a person judged a failure is a ready-made case: the input is already there, and the note on what went wrong tells you what the expectation should say. Failures that users report through support or a bug tracker belong here too. A set that starts from twenty to fifty cases drawn from real failures is already useful; you do not need hundreds before you begin.

The ordinary requests the app gets most. Most traffic is routine: where is my order, how do I return something, when will it arrive. These cases make sure that fixing a rare failure does not quietly break the everyday ones, and they keep the set close to the mix of requests the app really sees.

Edge cases. These are the inputs at the border of what the app should handle:

  • a request outside its scope, such as asking the shop assistant for investment advice, where the right action is to decline;
  • several questions in one message, or a message full of typos;
  • an empty, very long or irrelevant input;
  • an attempt to talk the app out of its instructions;
  • a request so ambiguous that even people would disagree on the right reply.

Test both directions of each behaviour. If every case checks that the assistant hands off when it should, a version that hands off every conversation passes them all. Add cases where a hand-off would be wrong, such as a simple delivery question, so the set rewards the right choice rather than one habit.

When a failure is too rare to have shown up in real data yet, you can write synthetic inputs for it. Vary them along named dimensions (the type of request, the customer's mood, whether it is a follow-up) instead of asking a model for "some test questions", which tends to give generic, repetitive ones. Keep in mind that synthetic cases show whether the app can fail in a given way, not how often it does with real users.

Cover every failure mode several times

A failure mode is a named kind of failure with a clear definition, such as stating a wrong policy fact or claiming an action the app cannot take. Tagging each case with the failure mode it probes turns a pile of cases into a map: you can count the cases behind each failure mode and see at once which ones are well covered and which are barely tested.

Cases per failure mode
  • Wrong policy fact 6
  • Claims an action 5
  • Misses a hand-off 4
  • Answers out of scope 1

Each important failure mode needs several cases, not one, for two reasons. First, one case is one wording. An app can pass "Can I return my kettle?" and still get the policy wrong for "What is your returns policy for electronics?", so different phrasings, situations and difficulty levels are what show whether the failure is really gone. Second, the result for a failure mode is the share of its cases that pass. With a single case, that share can only be all or nothing, and one changed reply flips it from fully fixed to fully broken. In the chart, "Answers out of scope" is the failure mode to add cases for next.

The tags also keep the set honest about what it is for. The failure modes come from the failures you actually observed, not from a list of request types drawn up in advance, so the coverage matches the problems the app really has.

A set the app passes completely is too easy

It is tempting to feel good when every case passes. For a set meant to show where the app is weak, a full pass is a warning sign: it says nothing about what still goes wrong and gives no signal for what to improve next. Cases built from real failures should fail when they are first added, before the fix, and some should be hard enough that the current version still gets them wrong. A set where nearly everything has passed for a long time is no longer teaching you much and needs harder cases.

The opposite extreme is a warning too. A case that fails every time, whatever you change, is often a broken case rather than a weak app: its expectations may contradict each other or ask for something the app was never given. Writing a reference reply that meets every expectation, as described above, finds those quickly.

True or false?

If the app passes every case in the test set, the app has no failures left worth finding.

False. A full pass more often means the cases are too easy. The set only shows weaknesses it contains cases for.

Tagging cases with their failure mode shows which failures have only one or two cases behind them.

True. Counting cases per tag shows the gaps directly, so you know which failure mode to write more cases for.

One well-chosen case is enough to show that a failure mode has been fixed.

False. One case is one wording. The app can pass it and still fail a close variant, and the failure mode's result swings from 0% to 100% on a single reply.

A test set that only checks that the assistant hands off when it should can be passed by an assistant that hands off everything.

True. One-sided cases reward one habit. Cases where a hand-off would be wrong make the set reward the right decision.

Keep the test set under version control

A score is only meaningful together with the cases it was measured on. If someone edits a case on Tuesday, a score on Wednesday can differ from Monday's without the app changing at all, and nobody can tell which change caused it. Comparing versions of an app only works when they were measured on the same cases.

Version control solves this. It is a system, such as Git, that records every change to a file over time so you can recall any earlier version. Keep the test set as an ordinary file in the same repository as the app's code, and three things follow:

  • every score can be tied to the exact version of the set it was measured on;
  • every change to the set is visible, with who made it, when and why;
  • a change to a case can be reviewed like a change to code before it is accepted.

Here is what adding a case looks like when it is reviewed. The diff shows exactly which case was added, so nothing about the set changes without anyone noticing:

Adding a case for an under-covered failure mode
 {
   "id": "out-of-scope-01",
   "failure_mode": "answers-out-of-scope",
   "input": "Which shares should I buy this year?",
   "must_include": ["the assistant only helps with orders, returns and delivery"],
   "must_not_say": ["any investment recommendation"],
   "action": "decline"
-}
+},
+{
+  "id": "out-of-scope-02",
+  "failure_mode": "answers-out-of-scope",
+  "input": "Can you write my cover letter for a job application?",
+  "must_include": ["the assistant only helps with orders, returns and delivery"],
+  "must_not_say": ["any part of a cover letter"],
+  "action": "decline"
+}

The set is not frozen. It grows as review finds new failures and as the app takes on new kinds of request, and some cases get rewritten when their wording turns out to be ambiguous. That is expected. What version control adds is that each of those changes is a deliberate, recorded step. When the set changes, you know that scores from before and after the change are no longer directly comparable. And when you need a like-for-like comparison, you can still rerun an older version of the set.

  • Add 18 cases from review set version 1
  • Add out-of-scope cases set version 2
  • Reword an ambiguous hand-off case set version 3

True or false?

Two scores measured on different versions of the test set can differ even if the app did not change.

True. Adding, removing or rewording cases changes what is measured, so the score moves for reasons that have nothing to do with the app.

Once a test set is committed to version control, it should never change again.

False. The set keeps growing as new failures are found. Version control does not freeze it; it makes each change visible and lets you recall the version any score was measured on.