Skip to content
mentoor

Topic 2 of 4 · 5 min read

Testing with tolerances, not exact matches

Goal: Rewrite a brittle exact-output test as an n-run test with a pass-rate threshold.

Replace "this input must produce this exact output" with "across N runs, at least this share must pass a check that accepts every valid answer." That turns a coin-flip test into a measurement.

Check yourself · 1 of 3

Which test is the most robust way to check a summary of a news article?

Choose one answer

Why it matters

An exact-match test on LLM output fails for harmless reasons (a synonym, a comma) and passes for the wrong ones (a lucky run on a broken prompt). Both erode trust. Engineers then ignore the suite, which is worse than having none.

How to do it

  1. Write the check around what must be true, not how it is said. Prefer structure and facts: valid JSON, a required field, a number within a range, a required fact present, a banned phrase absent. Use a model grader only for what code cannot judge, and keep that grader's own rubric short.
  2. Run each case N times. Five is a common start for a unit-level check; raise it when the pass rate sits near the threshold. Record each result, not just the average.
  3. Set a pass-rate threshold from data. Run the current good version 20 times, see where it lands (say 19 of 20), and set the bar below that with a margin. A bar of 100 percent on a stochastic feature will fail by chance alone.
  4. Separate hard rules from soft quality. A leaked secret or malformed JSON must fail on one occurrence. Tone and completeness can use a rate.
  5. Report the rate and the sample size. "18/20" carries more information than "pass". Miller's paper on error bars explains why small samples give wide uncertainty, so a drop from 19/20 to 17/20 may be noise.

Before and after

Before:

def test_summary():
    out = summarize(ARTICLE)
    assert out == "The council approved the budget on 4 May."

After:

def test_summary_keeps_key_facts():
    runs = [summarize(ARTICLE) for _ in range(10)]
    ok = [("budget" in r.lower() and "4 May" in r and len(r) < 300) for r in runs]
    assert sum(ok) >= 9, f"{sum(ok)}/10 passed"

The second version accepts any wording that keeps the budget and the date, within a length limit, and it fails with a count you can act on.

What goes wrong

  • A check that is too loose. Looking for the word "budget" passes a summary that gets the date wrong. Check every fact that matters.
  • A threshold set by hope. Pick it from measured runs of a version you trust.
  • A model grader that varies as much as the thing graded. Pin its model, keep it simple, and spot-check its verdicts by hand.
  • Too few runs. With three runs, one unlucky draw moves the rate by a third.

Examples

Extracting an invoice total

import json

def test_invoice_total():
    results = []
    for _ in range(8):
        data = json.loads(extract(INVOICE_TEXT))
        results.append(abs(data["total"] - 1284.50) < 0.01)
    assert sum(results) >= 7

The JSON must parse every time (an exception fails the test at once), and the total must be correct in at least 7 of 8 runs.

Choosing the threshold

The current prompt scored 19, 18, 19, 20 and 18 of 20 on five separate batches. The mean is about 18.8. A bar of 17 of 20 allows normal noise and still catches a drop to the mid-teens. A bar of 20 of 20 would have failed three of the five good batches.

Key points

  • Test properties that must hold (structure, facts, bounds), not the exact wording of the output.
  • Run each case several times and assert on a pass rate, not on a single run.
  • Set the threshold from measured runs of a version you trust, with a margin below its observed rate.
  • Hard safety rules fail on one occurrence; quality rules can use a rate.
  • Report the count and sample size, because small samples carry wide uncertainty.

Common mistakes

  • Asserting equality with a stored string.
  • Requiring 100 percent on a stochastic check.
  • Checking only that a keyword appears and calling the answer correct.
  • Using three runs and treating the result as stable.

Check yourself · 2 of 3

Your n-run test passes 9 of 10 runs on a known-good prompt. Which threshold is the most sensible starting bar?

Choose one answer

Check yourself · 3 of 3

Which check should fail the build on a single bad run, rather than use a pass rate?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic