Part 08›
✎

04. Testing Without Oracles#

Level: Practitioner · Reading time: 26 min · Exercise: 60 min Prerequisites: Quality Assurance Basics and the Oracle Problem

In one sentence: After this chapter you can write tests for a model-based feature that need no labeled answers, by describing how outputs must relate to each other and how inputs may change without changing the answer.

Why this matters#

Labeling tickets is slow. Suppose an agent labels about two hundred tickets per hour. A test set of ten thousand then costs fifty hours. It is stale in three months.

Meanwhile you have a pile of unlabeled tickets and four requirements that no label can check:

None of these asks "what is the correct queue?". Each asks whether two answers are consistent. This chapter covers the tests that answer such questions, plus four related strategies: behavioral testing, differential testing, simulation, and exploratory audits.

The core idea#

Chapter 01 defined a test oracle as whatever tells you the expected output. Many requirements do not need one, because they describe a relationship between outputs instead of the output itself.

An invariant is a rule that must hold for a model's outputs without saying what those outputs are. It is a partial specification. The useful form for models is a metamorphic relation: take an input, change it in a prescribed way, and state how the two outputs must relate.

for every input x:   h(f(x)) = f(g(x))

Here f is the model, g changes the input, and h says how the two outputs relate. For "a footer must not change the routing", g appends the footer and h is "unchanged". For "negation flips sentiment", g inserts a negation and h flips the score.

Figure 1. One metamorphic relation: the same model sees the original input and the changed one, and only the two outputs are compared.

flowchart LR
    X["Input x"] --> G["g: change the input in a prescribed way"]
    X --> F1["Model f"]
    G --> F2["Model f"]
    F1 --> O1["Output for x"]
    F2 --> O2["Output for changed x"]
    O1 --> H["h: do the two outputs relate as required"]
    O2 --> H
    H -->|"yes"| P["No violation"]
    H -->|"no"| V["Violation counted in the rate"]

No expected answer is needed anywhere on this path: the test compares two outputs of the same model with each other.

The analogy is property-based testing and fuzzing in ordinary software, where the most common invariant is "does not crash". Fuzzing means feeding the program random inputs to find crashes. The analogy breaks on strictness. For software, one violation is a bug. For a model, you usually measure how often the relation is violated and compare that rate with a threshold.

How it works, step by step#

Strategy 1: Behavioral testing#

Behavioral testing builds test data for one behavior at a time. A behavior is a capability you expect the model to have, such as "recognizes a refund request however it is phrased". Kästner calls behaviors partial specifications and describes this as unit testing for models (chapter 15).

  1. List behaviors. Work top-down from requirements and domain knowledge, or bottom-up from mistakes you have seen. For the triage model: handles typos, ignores signatures, separates two requests in one message, handles mixed German and English.
  2. Build data for each behavior. Four ways, cheapest first: - Templates. Fill a pattern such as "I was charged twice for {product}, please {action}". - Transformations. Take an existing input and change one thing: swap a word for a synonym, add a greeting. - Slicing. If your test set is large, filter out the inputs that need the behavior. - Hand-written examples. Slow, but best for rare and difficult cases. An LLM can draft them. Review every draft.
  3. Assert a rate per behavior. A behavior is learned to a degree, not as a yes or no.

Strategy 2: Invariants on unlabeled data#

Kästner sorts the useful invariants into three families (chapter 15):

Family The model should Examples
Robustness Give the same answer after irrelevant noise Typos, added whitespace, a footer, a re-encoded image
Independence Not use a specific signal Gender, a scanner ID, a template only one team uses
Domain expectation Follow a rule of the field More income never means less credit. Negation flips sentiment.

Where do the inputs come from?

Strategy 3: Differential testing#

Differential testing compares the model's output with a reference: an implementation known to be good, or a majority vote among several models. Kästner finds this most useful when the reference is accurate but too slow or expensive to run in production (chapter 15). A strong model becomes the reference for a small, cheap one trained to replace it. The limit is shared mistakes: models trained on the same data learn the same shortcuts, so they can agree and still be wrong.

Strategy 4: Simulation#

A simulator builds the input from a known answer, which reverses the oracle problem. A self-driving system is tested on rendered scenes where the testers placed every pedestrian, so they know where each one should be detected. For agents, a sandboxed environment with a known goal state plays the same role (see agent infrastructure). Simulators are expensive to build and never fully realistic, so they pay off only for high-stakes or hard-to-reproduce situations.

Strategy 5: Exploratory audits#

Red teaming puts experts to work attacking the system on purpose. End-user audits do the same with real users from minority groups. Kästner reports the critics' view, that such audits can look like theater, and argues for a narrower use: they are a good way to discover requirements you missed. Each finding becomes a slice, a behavioral test, or an invariant (chapter 15).

The term comes from software security: testers who act as attackers, as in penetration testing. Behavioral testing starts from requirements. A red team has no such list: experts who know the common attacks probe a system step by step, on intuition, to learn what it can be made to do. Large general-purpose models such as LLMs are now red-teamed to find ways around their safety mechanisms. An end-user audit has a narrower target. Before a wide release, users from minority groups try the system to find discriminatory behavior the developers did not think to test (chapter 15). For an agent, see Red-Teaming and Safety Evaluation.

Worked example#

Invariant tests for the triage feature. A stand-in model keeps the file runnable with no API calls and no cost. The pytest code is illustrative: it uses pytest.mark.parametrize in its long-standing form and was not re-checked against the documentation for this chapter.

# triage_stub.py -- stands in for the real model. It has one bad habit.
import re

class StubTriage:
    RULES = {"refund": "billing", "password": "account", "crash": "technical"}

    def route(self, ticket: str) -> str:
        if "unsubscribe" in ticket.lower():  # bad habit: it reads the footer
            return "other"
        for word, label in self.RULES.items():
            if word in ticket.lower():
                return label
        return "other"

def tidy(text: str) -> str:
    """Lower-case, drop punctuation, collapse whitespace. Meaning unchanged."""
    no_punct = re.sub(r"[^a-z0-9 ]", "", text.lower())
    return re.sub(r"\s+", " ", no_punct).strip()
# test_invariants.py
import pytest
from triage_stub import StubTriage, tidy

model = StubTriage()
UNLABELED = [  # no labels anywhere: these are raw tickets
    "I want a refund for order 42",
    "How do I reset my password?",
    "The app crashes every time I open settings",
    "Do you ship to Iceland?",
]
FOOTER = "\n\nTo unsubscribe from these emails, click here."

# Strict invariant: for every input, the answer is stable under clean-up.
@pytest.mark.parametrize("text", UNLABELED)
def test_punctuation_does_not_change_routing(text):
    assert model.route(tidy(text)) == model.route(text)

# Strict invariant that the stub fails on its first ticket.
@pytest.mark.parametrize("text", UNLABELED)
def test_footer_does_not_change_routing(text):
    assert model.route(text + FOOTER) == model.route(text)

# Rate-based invariant: most real models are only mostly stable.
def test_typos_rarely_change_routing():
    flips = sum(model.route(tidy(t)) != model.route(t) for t in UNLABELED)
    assert flips / len(UNLABELED) < 0.05

What to observe:

What the practitioners say#

Pitfalls#

  1. Writing a false invariant. "Adding text must not change the routing" is wrong when the added text is a second request. Fix: make the change provably irrelevant, and review relations with a domain expert.
  2. Asserting strictly over a model that is only mostly stable. The suite goes red permanently and gets ignored. Fix: strict for hard requirements, a rate for learned behavior.
  3. Comparing raw generated text. Two valid summaries always differ in wording. Fix: compare a parsed field or a specific property.
  4. Testing only clean inputs. Invariants pass on tidy data and fail on pasted logs and HTML. Fix: pull inputs from real traffic.
  5. Reading agreement between two models as correctness. Fix: treat disagreement as a list of inputs to inspect, not as a verdict.
  6. Counting adversarial findings as a quality score. The number depends on search effort. Fix: use the findings to debug, and measure the violation rate on realistic data.

Try it#

Choose one component and write five metamorphic relations for it.

  1. For each relation, write down g (the input change) and h (the expected relation between outputs).
  2. Collect 200 unlabeled real inputs.
  3. Run all five relations over all inputs and record the violation rate for each.
  4. For every violation, decide: the model failed, or the relation was wrong.

You are done when you have a table with five rates, and a written verdict for each violation. Stretch goal: write a small search that changes one input at a time until a relation breaks, and report the smallest change that did it.

Key takeaways#

Self-check#

  1. Write the footer requirement in the form h(f(x)) = f(g(x)). What are g and h?
  2. Why does random noise make poor test input for most real systems?
  3. A cheap model agrees with an expensive one on 99 percent of inputs. What does that not tell you?
  4. When is a strict assertion the wrong choice for an invariant test?
  5. A red team finds one serious failure mode. What is the durable output of that finding?
Show answers 1. `g` appends the footer to the ticket. `h` is the identity: the routing is unchanged. 2. Random inputs are mostly unlike production traffic. The system rejects them or treats them trivially, so violations on them say little about real use. Templates and generators produce realistic inputs and outliers. 3. It does not tell you the answers are correct. Models trained on similar data can share the same mistake and agree on it. 4. When the behavior is only mostly stable. A strict assertion then fails forever on a rare, tolerable violation, and the suite stops being informative. Use a rate against a threshold. 5. A new requirement, expressed as a slice, a behavioral test, or an invariant, that runs from then on.

Sources#

Next#

→ Testing Generative and LLM Components, because when the requirement is about the quality of generated text, the oracle has to be a rubric.