Reading a failure
A failure is the most useful thing proptastic prints: it’s a bug, with the smallest input that shows it and a line that keeps it from coming back. Each line has one job, and this page says what each is for.
The lines
Section titled “The lines”A bank charges one percent on a transfer, rounded down, but keeps the fee rounded to the nearest cent. A bank account finds that with a property, which fails with this:
expected: <50> but was: <51>
aTransferWithAFeeKeepsTheTotal failed after 2 tries (seed 0x4f1c2a9e7b3d5f60) shrunk counterexample: aliceHas = 50, bobHas = 0, amount = 50 fails for any: bobHas pin it: .example(/* aliceHas */ 50L, /* bobHas */ 0L, /* amount */ 50L) rerun: PROPTASTIC_SEED=0x4f1c2a9e7b3d5f60 ./gradlew test --tests 'BankAccountTest.aTransferWithAFeeKeepsTheTotal'- Your assertion’s own message comes first, untouched. proptastic adds to a failure; it doesn’t replace it.
- The header names the test, how many tries it took to fail, and the seed. The seed decides every input the run draws.
shrunk counterexampleis the smallest input that still fails the same way: here alice has 50 cents, bob none, and alice sends 50. Values are written as you’d say them,50, and a record’s components are named in a comment after it. Shrinking says how it got there. It readscounterexample:withoutshrunkwhen shrinking was off, or the input was already as small as it goes.fails for anynames the values that played no part: with the others held, the property fails whatever bob has. So the bug is in the fee, not his balance. Which values mattered says how it knows.pin itis a line to paste afterforAll(…). It’s Java source, so alongis50Lhere, and each value has a comment naming the draw it’s for. Pinned, that input runs first every time, so the bug can’t come back unnoticed.rerunruns this one test again with the same seed, so it draws the same inputs. It’s written in your build’s own command, or the CLI’s.
When a value spans lines, or a line would be wider than the terminal, every fact goes on a line of its own with its value indented below it. The words are the same.
A pinned example that fails
Section titled “A pinned example that fails”A pin runs before any random try. When one fails, the header says which:
@Testvoid sortBySetKeepsSize() { forAll(lists(integers())) .example(List.of(0, 0)) .check(xs -> assertThat(sortBySet(xs)).hasSameSizeAs(xs));}Actual and expected should have same size but actual size is: 1while expected size is: 2Actual was: [0]Expected was: [0, 0]
sortBySetKeepsSize failed on example 1 of 1 counterexample: xs = [0, 0]It isn’t shrunk: it’s the input you chose, so it’s shown as you wrote it, and there’s nothing to pin or rerun. Its header names no seed, since no seed drew it.
A value with no source form
Section titled “A value with no source form”proptastic writes a value as Java source when it can: numbers, strings, lists, records, enums. A class of your own that isn’t a record has no source form it can write:
/** A class, not a record: proptastic can't write one as Java source. */static final class Basket { private final List<Integer> prices;
Basket(List<Integer> prices) { this.prices = List.copyOf(prices); }
int total() { return prices.stream().mapToInt(Integer::intValue).sum(); }
@Override public String toString() { return "Basket" + prices; }}
@Testvoid aBasketCostsUnderAThousand() { forAll(lists(integers().atLeast(0).atMost(500)).map(Basket::new)) .check(basket -> assertThat(basket.total()).isLessThan(1_000));}Expecting actual: 1000to be less than: 1000
aBasketCostsUnderAThousand failed after 4 tries (seed 0x5eed) shrunk counterexample: basket = Basket[500, 500] pin it: .seed(0x5eed) // no source form for this input rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'PinsTest.aBasketCostsUnderAThousand'The counterexample is the value’s toString, shrunk as any other, since shrinking works on the
choices the value was drawn from. The pin is the seed, which draws the same input again, with a
comment saying why. To pin the input itself, say how to write it as source on its domain,
.map(Basket::new).writtenAs(basket -> "new Basket(" + … + ")"), as
a type from another library does; or draw a record,
or draw the record’s parts and build the class in check.
A failure that doesn’t come back
Section titled “A failure that doesn’t come back”When an input fails and then passes when it runs again, the failure is said to be flaky, and proptastic says so rather than shrink it. Shrinking shows one.