Skip to main content

Testing code when you don’t know the right answer

9 min read

A test needs an expected output, and most of the time you have one: 2 plus 2 is 4, an empty cart costs nothing and so on. But what do you test against when nobody knows the right answer? Not you, not the person using the app, not anyone who could look at the data.

That was the case with an engine I built for Digest, the food journal I'm working on: it reads a journal and outputs a list of foods that might be behind your symptoms. Give it a journal and a list comes out, but is it the right list?

The person who logged the meals doesn't know, that's the whole reason they'd want the list, and I don't know either. The data is the truth, if you will.

I also won't look at real journals to find out. I'm very privacy-focused, I don't want to handle any data I don't need, I run no analytics. Digest also contains a very private kind of information, which I'd rather not touch. And even if I did look, a real journal just doesn't come with an answer key.

Search for this and the answer you'll probably find is approval testing, also called characterization testing: run the code, look at what comes out, approve it, and fail any later run that comes out different. It's a pretty good tool for legacy code you're about to refactor, where today's behavior is the spec. For an engine whose job is to be right about data nobody can see, it just takes today's guess and protects it.

Testing has a name for this, the test oracle problem. An oracle is whatever tells you the correct output for a given input, and some code simply doesn't have one.

So instead of looking for the answer in the data, I write the data around an answer.

(The engine itself didn't make it into the app in the end, for unrelated reasons, but the approach is still worth a mention, and I learned from it.)

Plant the answer

For Digest, I invented journals where I'd decided up front which food is behind the symptom, how strongly and how often it's eaten, and then checked whether the engine named that food. Statisticians call this a simulation study (generate data from a model where you know the truth, then measure how much of it the method recovers).

The truth is planted, so “correct” is knowable rather than argued.

I had a handful of these generators by the end, each planting a different shape of answer: a food eaten mostly inside another one, like milk in coffee; a menu that rotates through the week; days of 3 to 6 eating events at uneven hours, the way a snacker eats and many more. Once a result depends on a generator, it stays as it is, and a new shape gets a new generator; a new setting on an old one would quietly change what the old results meant.

You need 2 kinds of planted data, though: data with an answer in it, and data with none. The first tells you whether the code finds what's there. The second tells you how often it finds something that isn't, and it's the one you can't skip, since code that flags everything would find every planted answer and look perfect. For Digest, that was a journal where nothing caused anything, so every food the engine flagged was wrong.

How you plant matters too. Say the answer is “1 in 4”. The obvious way is a coin flip per item, each with a 1 in 4 chance, but then 24 items hold 6 hits on average, while any single dataset might hold 4 or 8, and every number you measure carries the generator's luck along with the code's. Planting exact counts fixes that: 24 items at 1 in 4 always hold 6 hits, and the random seed only decides which 6.

The bar, written before the run

Planting the answer gets you numbers, and numbers are a bit too easy to make peace with once you've seen them. Is a check that's wrong 14% of the time good? It depends on what you were hoping for, and if the hope gets written after the number, it'll probably fit the number.

So before a run, I write down the question, what gets compared, and the bar: which result counts as passing. Research calls this pre-registration, where you file the hypothesis and the analysis before collecting any data, so the goalposts can't move afterwards.

It matters most when a fix almost works. Say the bar allows a wrong answer in at most 10% of runs, the code gets it wrong 23% of the time, and a fix brings that down to 14%. Read cold, that's a fix; against the 10% written down before the run, it's a miss. That exact case happened in Digest, so the fix didn't ship, and the engine took a different approach to that situation.

The misses stay in the plan, too, with lines like “Predicted wrongly, recorded as such”. A bar written down first won't make you right, but it does show you when you weren't.

Invented people

Planted data usually changes one thing at a time, which is what makes each result readable, and also why it can't tell you how the whole thing behaves for a real person, with everything happening at once. That's what I ended up asking about Digest: what would it show someone with several culprits of different strengths, after a few weeks, among ordinary meals and symptoms that have nothing to do with any of them? Nothing I had measured could answer it.

The answer was a different kind of planted data: whole lives, written by hand. Cem Kaner has a name for this kind of test, a scenario test: realistic, complex and easy to tell pass from fail, among other things. One of my rulings in the plan reads “Hand-written people, not a sampled population. […] A diverse sample a person can picture beats distributions nobody can check.”

There were 20 of them, each with its right answer written beside it before any run. There was the textbook case, someone whose culprit only shows up at Friday and Saturday dinners, a snacker with 5 eating events a day, 3 people with nothing to find, where the right answer was for the engine to stay quiet, and so on.

Invented cases have a quiet trap, though: they share whatever their author assumed. I had the first list reviewed by 2 fresh Claude Code sessions with no context, one reading it against how the engine works and one against how real people eat, and the second found that most of the list was one standard case with a single detail changed per person: different names on the surface, the same habits underneath. It's more or less the same trap as the premise I wrote about, one assumption nobody chose sitting under every case, only spread across a whole list.

What it can't claim

A list of 20 invented people can't say how well the engine works for real ones, and the plan says it in its bluntest line: “Not for absolute claims. The people are invented and always will be: no analytics, ever.”

What are they good for, then? Comparison: the same people before and after an engine change, with/without a setting.

The planted answer also only covers what someone thought to plant, but given enough scenarios, you can cover a significant portion of what will happen in the real world.

Claude Code's part

Claude Code wrote the generators, the probes and the round write-ups, and ran the rounds; I asked the questions and made the calls, which the plans record as “owner asked” and “owner rulings”. This is something which would've been a monumental effort to do by hand.

A probe is a test that's disabled by default and run on demand. It builds thousands of journals and writes its results as a table to a file, since neither assertion output nor print() makes it out through xcodebuild (the simulator post has more on that). Claude reads the table and writes the result into the plan next to the bar, and I decide what happens next: ship the change, drop it, or ask a new question.

I think pre-registering matters even more with a model. For the fix that missed its bar, Claude ran 345,600 tests in about 20 minutes and handed me a fluent reading of them, and if the bar hadn't been written down already, that reading would most likely have picked one for me. I said in an earlier post to define “done” before the work starts; a bar written before the run is the same advice, for numbers.

If you have code whose output nobody can check, start with data that holds nothing, then data that holds one answer you planted, and write down what passing looks like for both before you run either.

The everyday tests, and the circular ones that only check code against itself, are in the previous post. If you've tackled this differently, I'd love to hear how (@roland.leth.ro / @rolandleth).

The next post moves from the engine to the App Store, automating the screenshots. The rest of the series is listed in Claude Code for iOS development.