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].
Specified oracles come from a specification: an expected value derived by hand from the requirement, conditions that the input and output must satisfy (preconditions and postconditions), or a formal model, a mathematical description of the required behavior.
Derived oracles come from something other than a specification. Differential testing §10.6 runs a second implementation on the same input and compares the outputs; a separately written program used in this way is called a pseudo-oracle. Metamorphic testing §10.8 checks a relation between runs instead of an expected value, for example that for every . A regression oracle compares the output with that of an earlier version.
Implicit oracles detect behavior that is wrong for almost any program: a crash, a hang, a failed assertion inside the program, or a report from a sanitizer §10.21, a tool that adds run-time checks for errors such as a memory access outside an object. They need no specification, and they catch only those kinds of failure.
No automated oracle. When none of the above exists, a person judges the output. Human judgment is slow, costly and itself error-prone, so it is used where nothing else works.
Four habits make oracles effective.
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.
For each oracle, state the property it checks. A crash detector checks that the program did not crash, not that its result is right.
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.
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 . A faulty implementation returns , computed by a helper twice(x).
Weak input. For , the required value is and the faulty value is . The outputs agree, so no oracle can detect the defect with this input, and the same holds for . This is coincidental correctness: the faulty code runs, but its output happens to be right.
Independent expected value. For , the tester works out by hand. The faulty function returns . Since , the test fails and exposes the defect.
Expected value that shares the implementation’s logic. The test computes its expected value by calling
twice(3), the same helper the implementation uses, and gets 6. The function returns 6, the test passes, and the defect stays hidden. Recording the function’s own output once and using it as the expected value from then on, a golden value, has the same effect.Metamorphic relation. Squaring must give the same result for and . The faulty function returns 6 for 3 and for . Since , the relation fails, and no expected value was needed.
Property. A square is never negative. The faulty function returns for , which breaks the property.
Implicit oracle. The faulty function never crashes, so the check “no crash” passes every input.
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 be the program under test, an input and the observed outcome. Let the specification be the set of pairs in which is a correct outcome for . An oracle maps a pair to pass or fail. It can make two kinds of error.
A false alarm §1.22: fails a correct outcome, that is, but .
A missed fault: passes an incorrect outcome, that is, but .
A complete oracle makes neither error. A partial oracle makes no false alarm but checks only a weaker condition, a set that contains . 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 are independent of when they were derived from the requirement by a route that shares no code, data, tables, tools or interpretation of the requirement with . A mistake in a shared source appears in and in alike, and 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 in the example, no oracle could tell from .
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.
Computed by calling the implementation or a helper it uses: not independent, because a defect in the shared code moves the expected value along with the output.
Recorded from an earlier run of the implementation, a golden value: not independent, so the test detects change, not wrongness.
Worked out by hand, or taken from published known-answer tests: independent of the code, but it can still share a misreading of the requirement, for example when the same person read the requirement for both.
Produced by a second implementation: independent only as far as its authors, sources and tools differ from those of the first.
For example, if sq(x) wrongly returns , a test that computes its expected value with the same helper expects 6 for and passes, while a hand-derived expected value of 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].