Targeted tries
Some bugs only show at the far end of something you can measure: the costliest basket, the longest queue, the deepest tree, the slowest reply. Random tries spread out over what a domain draws, so they seldom get that far, and a property can pass run after run while the bug waits at an end no try reaches.
Say delivery is free for a basket under 150.00, and the code gives it to every basket. The bug is real, but only a basket of fifteen or so items near the top price shows it, and a basket is drawn about 7 items long, each anywhere from 0 to 1,000 cents. In proptastic’s own tests, random tries reached 15,000 cents from 5 of 40 seeds, and 25,000 from none, even in 400 tries each, where targeted tries reached it from 15 or more.
Give each try a score
Section titled “Give each try a score”target(score), called inside the check, says how far along a try got: here, the basket’s total.
The run then aims for higher scores instead of hoping to land on one. The classify line only
counts how many tries got past 10,000 cents, to show the climb:
@Testvoid deliveryIsFreeForEveryBasket() { forAll(lists(integers().atLeast(0).atMost(1_000))) .check( basket -> { target(total(basket)); classify(total(basket) >= 10_000, "10,000 cents or more"); assertTrue(deliveredFree(basket)); });}expected: <true> but was: <false>
deliveryIsFreeForEveryBasket failed after 48 tries (seed 0xf) shrunk counterexample: basket = [ 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000] pin it: .example( List.of( 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000)) rerun: PROPTASTIC_SEED=0xf ./gradlew test --tests 'TargetTest.deliveryIsFreeForEveryBasket'
classify tries 10,000 cents or more 15 31.3% ██████▎
sizes p50 p90 p99 basket 13 32 32It failed after 48 tries, from seed 0xf. The table under the failure is the climb: 15 of those
tries, nearly a third, cost 10,000 cents or more, where a random basket of about 7 items seldom
comes near. They’re the climb: tries that started from the dearest basket so far and changed it,
until one passed 15,000.
The counterexample is an ordinary one: proptastic shrank the basket the climb found, as it shrinks
any failure, to the fewest items that still cost too much, and its pin it and rerun lines work
as always.
How it climbs
Section titled “How it climbs”A run with a score works in two parts:
- The first quarter of its tries are random, drawn as they would be with no
targetat all. Each try’s score is kept, with the choices that drew it. - Every try after that starts from the best so far. It takes the best-scoring try’s choices, changes one to three of them, each up or down a little, doubled, halved, to the most it can be, to 0, or anywhere, and runs the basket those choices make. If it scores higher, it’s the new best.
So a list that’s long and dear gets a little longer or dearer, step by step, where a fresh random basket would start again from about 7 items. Because the changes are to choices, every changed try is still a basket the domain could draw, whatever the domain is.
The changes come from a random stream of their own, keyed on the run’s seed. So the random quarter
draws exactly what it would have without a target, and the same seed climbs the same way again:
the rerun line reproduces it.
Several scores
Section titled “Several scores”A property can climb more than one thing. Give each score a label, and the climb takes each label’s best try in turn:
target(total(basket), "total");target(basket.size(), "items");When it helps
Section titled “When it helps”A score helps when the bug grows with something you can count, and gets closer as the count grows: a total, a size, a depth, a time, a number of retries. The better a score tracks how near a try is to failing, the faster the climb.
It costs nothing extra: the climb uses the tries the run makes anyway, three quarters of them in place of random ones. And a property in CI runs on a new seed each time, so each run climbs from a new start.
When it doesn’t
Section titled “When it doesn’t”- The failures are scattered. If failing inputs don’t sit at the end of anything measurable, there’s nothing to climb toward, and random tries find them as well.
- The score is flat. A score that’s 0 until the bug and 1 at it gives the climb nothing to follow. Score how close a try got, not whether it failed.
- The domain can draw it straight away. If the inputs you need are simply rare, change the
domain: wider bounds, longer lists with
averageSize(…), or the values themselves as edges. That reaches them every run, where a climb reaches them from some seeds. - You want the pass to count. Only random tries count toward a pass’s
how sure:line, as a climbed try was chosen, not drawn, so a targeted run’s line counts only its random ones.
A climb can also stall: it only ever looks near the best try so far, so a best that’s a dead end holds it there until the run ends. A new seed is a new start.
This is targeted property-based testing, from Löscher and Sagonas, 2017.