Classify and require
A pass says nothing about inputs that were never drawn. If the case you care about, a full
basket, a leap year, an order at the limit, comes up once in a thousand tries, a pass hardly
checked it. classify shows you how often each case came up, and require fails the run when
one that matters is too rare.
Look at what you draw
Section titled “Look at what you draw”classify(…) counts the tries a condition holds for:
@Testvoid quantitiesReachBothEnds() { forAll(quantities()) .check( quantity -> { classify(quantity == 1, "the least"); classify(quantity == 10_000, "the most"); classify(quantity > 1 && quantity < 10_000, "between"); });}A failure prints the counts under it, and under them the sizes its lists, sets, maps, strings
and byte strings came out, as percentiles: half were at most p50, nine in ten at most p90.
Run with PROPTASTIC_STATISTICS=on and a pass prints both too.
Ask for a share with require
Section titled “Ask for a share with require”Where a case matters, say how often it must come up, and the
run fails when it doesn’t. require(20, …) asks for at least 20% of tries:
@Testvoid fullBasketsComeUp() { forAll(lists(integers().atLeast(1).atMost(99))) .check( basket -> { require(20, basket.size() >= 20, "twenty items or more"); assert total(basket) >= basket.size(); });}fullBasketsComeUp covered too little in 800 tries (seed 0x5eed) too rare: "twenty items or more" in 57 of 800 tries (7.12%), needs 20% rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'RequireTest.fullBasketsComeUp' ! draw it more often: weighted(…), .edges(…), averageSize(…) or a domain of its own or check one input you know every run: .example(…), in place of require
classify tries twenty items or more 57 7.1% █▍
sizes p50 p90 p99 basket 5 17 31A basket is about 7 items long, so twenty or more comes up in about 7 tries in 100, far short of the 20% asked. Here the fix is longer baskets:
@Testvoid fullBasketsComeUpWhenBasketsAreLonger() { forAll(lists(integers().atLeast(1).atMost(99)).averageSize(20)) .check( basket -> { require(10, basket.size() >= 20, "twenty items or more"); assert total(basket) >= basket.size(); });}How sure it has to be
Section titled “How sure it has to be”A run counts a sample of tries, so a share drawn at exactly what’s asked would look short half the
time by chance alone. So require works to a line a little below what’s asked, nine tenths of it,
and calls a share only once the tries make it sure which side of the line it’s on:
- Too rare, and the run fails, when the share is surely below the line: 18% for the 20% above.
- Enough when it’s surely at or above the line.
- Not yet otherwise, and the run draws as many tries again, doubling up to 64 times those asked.
“Surely” is one in a billion: a share at or above the line is called too rare less than once in a billion runs, and one below it called enough as rarely. Each call takes as many tries as that needs, so a share far from the line is told in the tries asked, and one near it takes more. The first run above, 7% against a line at 18%, was told after 800 tries.
PROPTASTIC_CERTAINTY changes how sure a call must be: 1e-6, one in a million, is told in fewer
tries, and wrong a thousand times as often.
When it can’t tell
Section titled “When it can’t tell”A share right at the line can’t be told either way in any number of tries. About 9% of these baskets hold 18 items or more, and 10% asked puts the line at 9%:
@Testvoid basketsOfEighteenComeUp() { forAll(lists(integers().atLeast(1).atMost(99))) .check( basket -> { require(10, basket.size() >= 18, "eighteen items or more"); assert total(basket) >= basket.size(); });}basketsOfEighteenComeUp passed, but its coverage is incomplete after 6,400 tries (seed 0x5eed) incomplete: "eighteen items or more" in 567 of 6,400 tries (8.86%), needs 10% leaning: neither way yet; a call needs 1 in 1,000,000,000 rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'RequireTest.basketsOfEighteenComeUp' ! more tries settle it, .tries(…), and so does drawing it more often: weighted(…), .edges(…), averageSize(…), or a share asked for that the tries clear ! PROPTASTIC_INCOMPLETE=fail fails the property on it, as for CIAfter 64 times the tries it still can’t say, so the share is incomplete. That’s never a silent
pass: the run says which labels it couldn’t tell and how sure it got, a leaning: line saying
which way the tries lean and how often a lean that strong would be wrong. The property passes, so a
build isn’t failed by chance, and proptastic run says it on its summary, its agent notes and its
events. In CI, where an untold share should stop the build, PROPTASTIC_INCOMPLETE=fail fails the
property instead.
To settle it, give the run more tries, draw the case more often, or ask for a share the tries clear: a share of 9% asked for as 10% is one to decide about, not to leave to chance.
Or check the one you know
Section titled “Or check the one you know”The ! line’s second half is for a case that matters as one input rather than as a share: the
full basket you saw fail last week, the year 2000, an order exactly at its limit. Give it as an
example, and it runs first in every run, whatever the seed, before any random try:
@Testvoid aFullBasketIsCheckedEveryRun() { forAll(lists(integers().atLeast(1).atMost(99))) .example(Collections.nCopies(20, 99)) .check(basket -> assertTrue(total(basket) >= basket.size()));}require asks how often the random tries reach a case, so an example doesn’t answer it: use one in
place of the other, or both when you want the case checked every run and drawn often too.
Why not a seed
Section titled “Why not a seed”A seed that happened to draw a full basket looks like the same fix, and isn’t. It pins one run’s
draws, and only while every domain draws exactly as it did. Widen a bound, add an edge or another
draw, and the same seed draws other inputs: the full basket quietly stops coming up, and nothing
says so. An example names the input itself, so it’s checked until you take it out. That’s why the
line offers .example(…), and never a seed.