Skip to content

Which values mattered

A shrunk counterexample is small, but small isn’t the same as relevant. When a property draws three values, the bug often depends on one or two of them, and the third is only there because it had to be something. proptastic checks which, so you know where to look.

The bank’s property draws alice’s balance, bob’s balance and the amount. Shrunk, it fails with alice’s 50, bob’s 0 and 50 sent:

Terminal window
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'

fails for any: bobHas says bob’s balance played no part: with alice’s 50 and the 50 she sends held, it fails whatever bob has. So the bug is in the fee on the amount, not in his balance.

Once shrinking has made the input as small as it goes, proptastic takes each value in turn and draws it afresh 20 times, the others held as shrinking left them. A value that fails the same way every time, the same bug, didn’t matter, and is named under fails for any. One that passed even once did matter.

It’s a check of 20 draws, not a proof: a value named there could still matter for some value it didn’t draw. It’s a hint about where the bug is, and is right far more often than not. You can change how many draws with PROPTASTIC_REDRAWS: more make the hint surer and the failure slower to report, and 0 turns the check off.

There’s no fails for any line when:

  • every value mattered, which is common, and worth knowing too;
  • fewer than half of a value’s draws counted, because the property rejected the rest as inputs it doesn’t apply to, so there’s too little to say;
  • PROPTASTIC_REDRAWS is 0, which turns the check off;
  • shrinking was off, or found nothing smaller, so there’s no smallest input to hold;
  • the test runs steps, which name their own.

Privacy