Shrinking
A random input that fails is rarely a good bug report. It’s a list of five prices in the hundreds, a string of forty characters, most of which have nothing to do with the bug. Shrinking finds a smaller input that fails the same way, so what you read is the bug and little else.
Before and after
Section titled “Before and after”A small basket, of up to five items, should cost under 100:
@Testvoid aSmallBasketCostsUnderAHundred() { forAll(lists(integers().atLeast(0).atMost(1_000)).ofSize(atMost(5))) .check(prices -> assertThat(total(prices)).isLessThan(100));}With shrinking off, PROPTASTIC_SHRINK=false, the run reports the first input that failed, as
it was drawn:
Expecting actual: 2892to be less than: 100
aSmallBasketCostsUnderAHundred failed after 1 try (seed 0x5eed) counterexample: prices = [988, 135, 704, 1000, 65] pin it: .example(List.of(988, 135, 704, 1000, 65)) rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'ShrinkingTest.aSmallBasketCostsUnderAHundred'With shrinking on, as it is unless you turn it off, the same run reports this:
Expecting actual: 100to be less than: 100
aSmallBasketCostsUnderAHundred failed after 1 try (seed 0x5eed) shrunk counterexample: prices = [100] pin it: .example(List.of(100)) rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'ShrinkingTest.aSmallBasketCostsUnderAHundred'One item at exactly 100: the boundary, and nothing else.
How it works
Section titled “How it works”proptastic doesn’t shrink values. It shrinks the choices the
domains drew them from: it drops choices and lowers them, and, knowing where each draw’s choices
start and end, drops or zeroes a whole element, entry or step at once. It runs the property on each
smaller sequence, and keeps a change only when the property still fails. Since choice 0 is the simplest
value in every domain, smaller choices make simpler inputs, and every sequence it tries is one
the domains could have drawn. It works the same for any domain, including your own map,
flatMap and records, with nothing to write.
So a counterexample shrinks toward these:
| Domain | Shrinks toward |
|---|---|
| a number | 0, or the bound nearest 0 |
a double |
a whole number, 0.0 if it can: doubles().atLeast(0.5).atMost(2.5) toward 1.0 |
elements(…), oneOf(…), an enum |
the first |
booleans() |
false |
| a list, set or map | as few elements as allowed, each shrunk too |
| a string | as short as allowed, each character toward a, or toward an alphabet’s first |
optionals(…) |
Optional.empty() |
The same bug, not another
Section titled “The same bug, not another”A smaller input that fails for a different reason isn’t kept. proptastic compares each failure with the first: the same type of exception, thrown from the same line of your test, or with the same message once the values in it are taken out. So a shrink can’t trade the bug you hit for an easier one nearby, such as an index out of bounds in place of a wrong total. A stateful test’s bug is the invariant that broke, by name.
Once the JVM has compiled a line that throws one of its own exceptions often, such as a null
pointer or an index out of bounds, it throws one made in advance, with no message and no stack.
proptastic counts that as the same bug as any of its type, so shrinking goes all the way without
-XX:-OmitStackTraceInFastThrow. When the smallest input fails that way, the report shows the
message and stack of the latest failure of the bug that had them, so it still says where it threw.
Everyday characters first
Section titled “Everyday characters first”Between two strings of the same length, the one with fewer characters past printable ASCII is simpler. A name longer than five characters shrinks to six plain letters, not to a mix that Java happens to count as six:
@Testvoid aNameFitsTheField() { forAll(strings()).check(name -> assertThat(name).hasSizeLessThanOrEqualTo(5));}Expecting size of: "aaaaaa"to be less than or equal to 5 but was 6
aNameFitsTheField failed after 3 tries (seed 0x5eed) shrunk counterexample: name = "aaaaaa" pin it: .example("aaaaaa") rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'ShrinkingTest.aNameFitsTheField'When a failure doesn’t come back
Section titled “When a failure doesn’t come back”Before shrinking, proptastic runs the failing input once more. If it passes the second time, the failure depends on something besides the input, and shrinking would chase it. So it stops and says so. This cache answers 0 the first time it’s asked for a price, and kept between tries, it’s wrong only once for each:
private final PriceCache cache = new PriceCache();
@Testvoid theCacheAnswersTheListPrice() { forAll(integers().atLeast(1).atMost(1_000)) .check(sku -> assertEquals(listPrice(sku), cache.price(sku)));}expected: <101> but was: <0>
theCacheAnswersTheListPrice failed on try 1, then passed when that input ran again (seed 0x5eed) counterexample: sku = 1 pin it: .example(1) rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'FlakyTest.theCacheAnswersTheListPrice' ! flaky: the same input passed and failed, so the result depends on more than the input: time, shared state, threads or iteration order. A pinned example will be flaky too.Look for what outlives one try: a field, a cache, a static, a file, the clock. Here it’s the
cache, which should be made afresh inside check, or start full.
Steps and threads
Section titled “Steps and threads”A stateful test shrinks its steps the same way: it drops steps and simplifies each one’s arguments, keeping only what still breaks the rule, and prints the steps that remain. Steps on threads shrink the steps, then the schedule, and move each step they can from a thread to the steps taken first, before the threads start, so what’s left on the threads is only what has to overlap.
After shrinking
Section titled “After shrinking”Once an input is as small as it goes, proptastic asks which of its values mattered at all, and
names the ones that didn’t under fails for any.
Which values mattered says how.
The idea of shrinking choices rather than values is from MacIver and Donaldson, 2020.