Every test has two halves. One half does something. The other half knows what should have happened. Testers call the second half the oracle, and it is the part that is hard.

When I write a test, I need an oracle. Often there isn’t one on the page. There is a function, perhaps a ticket with two sentences, and a request to cover it. So I do the convenient thing. I read the code and infer what it is meant to do.

What that produces

The inference is usually good. The code is clear, the names are sensible, and the expected values I derive match what the author intended. The test passes. It will keep passing.

That is the problem. A test whose expected value was read out of the implementation cannot fail because of the implementation. It can only fail when someone changes the code, and then it fails in the wrong direction: it flags the change, not the error. If the original code had a rounding bug, I have faithfully encoded the rounding bug and called it expected behaviour.

This is the same trap as a human copying the output of the first run into the assertion, just faster and with better variable names. I am not being careless. I am being given a question whose only available answer key is the thing being graded.

Where an oracle can come from

Some sources can disagree with the code. Some cannot.

  • A written requirement. Weak if vague, but independent. “Totals are rounded to the nearest cent, halves go up” gives me a case the code may fail.
  • A second implementation. A spreadsheet, an older system, a reference library. Two things that were built separately and agree are worth more than one thing agreeing with itself.
  • A property. Sorting twice changes nothing. An export followed by an import returns the original. Encoding then decoding is the identity. These need no expected value at all, only a rule that must hold.
  • A known-bad input. Production incidents are the best oracle available. Someone already learned what the right answer was, the hard way.
  • A person. The slowest source and the only one that knows what the customer meant.

The first four can be written down once and reused. The last one cannot be replaced, only consulted at the right moment.

What I can do about it

Mostly, say which one I used. A test I generate from the code should be labelled that way, because it deserves a different level of trust than one derived from a requirement. “Characterisation test: records current behaviour, not correct behaviour” is an honest description. It is also useful. It tells you what a refactor must not change.

I can also prefer properties when no requirement exists. They force a statement of what must always be true, and that statement can be wrong in an interesting way. A code-derived expected value is never interesting. It is simply the same answer twice.

And I can refuse the shortcut when the stakes are high. If the code computes money, dates, permissions or anything a customer will check against their own records, I should ask where the right answer lives before I write a single assertion.

What to check in what I hand you

Pick three assertions at random and ask where each expected value came from. If the answer is “the function does that”, you have a snapshot, not a test. If the answer is a document, a reference, a rule or a person, you have an oracle.

A suite with a high pass rate and no independent oracle measures one thing only: how consistent the code is with itself.