Section 10.5

Test oracle

Definition

A test oracle is the rule or mechanism that decides whether the behavior observed in a test is correct. The test supplies an input and runs the program. The oracle takes the input and the observed output or state and returns pass or fail. An oracle can be an expected value worked out by hand, a specification, a second implementation, a relation that several runs must satisfy, or a general check such as “the program did not crash”. Without an oracle, running a program shows only that it ran.

The idea

Producing inputs is easy. Fuzzing §10.9, which runs a program on large numbers of automatically generated inputs, can try millions of inputs per hour. Deciding whether each output is correct is the hard part. A routine that finds the shortest route between two points on a map can be run on a million pairs of points, but nobody has the shortest route for each pair at hand to compare with. The difficulty of obtaining an oracle is called the oracle problem.

An exam with an answer key is a fair picture. The students’ answers are the outputs, and the answer key is the oracle. A key copied from one student’s paper marks that student’s mistakes as correct, which is why the key must be worked out independently of the answers it judges. An oracle can also be partial, like a marker who checks only that each answer has the right units: that catches some wrong answers without knowing any right one. The picture stops at one point. An exam key gives every right answer, while most practical oracles check only some properties of the output.

How it works

A 2015 survey of the oracle problem by Barr, Harman, McMinn, Shahbaz and Yoo sorts oracles into three kinds, and treats separately the case in which none of them is available [486].

Four habits make oracles effective.

  1. Separate the generation of inputs from their judgment. A large run of random inputs with no oracle for the property of interest can execute a defect without detecting it.

  2. For each oracle, state the property it checks. A crash detector checks that the program did not crash, not that its result is right.

  3. Derive expected values independently of the implementation: by hand, from the specification, or from published known-answer tests, inputs printed together with their correct outputs, such as the test vectors in many cryptographic standards.

  4. Check every effect the requirement names: the returned value, the changed state and side effects such as stored data, not only a success status.

Some oracles judge a result without computing it. A routine that sorts a list of numbers is correct for an input exactly when its output is in order and contains the same numbers as the input, each the same number of times. Both conditions are cheap to check, so this oracle is complete for sorting. The first condition alone is a partial oracle: a routine that always returns an empty list passes it.

Worked example

A function sq(x) must return x×xx \times x. A faulty implementation returns x+xx + x, computed by a helper twice(x).

Three of the five oracles exposed the defect: the independent expected value, the metamorphic relation and the property. The expected value that shared the implementation’s helper and the crash check did not. With the input 2, no oracle could.

Stated precisely

Let PP be the program under test, xx an input and y=P(x)y = P(x) the observed outcome. Let the specification be the set Spec\mathrm{Spec} of pairs (x,y)(x, y) in which yy is a correct outcome for xx. An oracle OO maps a pair (x,y)(x, y) to pass or fail. It can make two kinds of error.

A complete oracle makes neither error. A partial oracle makes no false alarm but checks only a weaker condition, a set Spec′\mathrm{Spec}' that contains Spec\mathrm{Spec}. With a partial oracle, a fail proves the outcome wrong, while a pass does not prove it right.

Independence is a condition on how the oracle was made; it has no formula. The expected results of OO are independent of PP when they were derived from the requirement by a route that shares no code, data, tables, tools or interpretation of the requirement with PP. A mistake in a shared source appears in PP and in OO alike, and OO then passes the wrong outcome. Such a shared mistake is called a common-mode error §19.2.13.

Limits and common mistakes

An oracle covers only the property it checks. A crash detector says nothing about whether results are right, whether an operation was permitted, or whether the program eventually responds.

Independent authors can share a mistake. In an experiment published by John Knight and Nancy Leveson in 1986, independently written versions of one program failed on the same inputs far more often than statistical independence predicts [440]. A second implementation is a strong oracle, not an infallible one, and both versions can share a misreading of the requirement.

An oracle can itself be wrong. A tolerance that is too tight for floating-point results, numbers stored with limited precision, produces false alarms; one that is too loose hides errors.

A golden value recorded from the implementation detects change, not wrongness: it preserves whatever the implementation did when the value was recorded.

A good oracle needs a good input. With x=2x = 2 in the example, no oracle could tell x+xx + x from x×xx \times x.

Some programs have no practical oracle at all, because the correct answer is what the program was written to find out. Elaine Weyuker called such programs non-testable in a 1982 paper [487]. Derived oracles, such as metamorphic relations and pseudo-oracles, are the usual way to test them.

An oracle that looks only at a status code misses wrong side effects. A routine that writes the wrong bytes to storage and then returns success passes such an oracle.

Questions answered

Is the expected result independent of the implementation under test?

The expected result is independent only if it was derived from the requirement by a route that shares nothing with the implementation: not its code, any helper it calls, its tables, its earlier output, or its author’s reading of the requirement. To decide, trace where each expected value came from.

For example, if sq(x) wrongly returns x+xx + x, a test that computes its expected value with the same helper expects 6 for x=3x = 3 and passes, while a hand-derived expected value of 3×3=93 \times 3 = 9 makes the test fail and exposes the defect. Record for each test where its expected value came from, and which assumptions it shares with the implementation.

Related entries: Differential testing (§10.6), Metamorphic testing (§10.8), Independent review (§10.44), Mechanism versus evidence (§1.37), Semantic oracle independence and common-mode errors (§19.2.13), False positive and false negative (§1.22), Property-based testing (§10.7). Further reading: [440, 486, 487].