Skip to content

Health checks

A property can pass while checking almost nothing: a filter that throws away most of what’s drawn, inputs so large each try takes ages, drawing slower than the check itself. None of that is a bug in your code, but each wastes the run, or quietly makes a pass worth less. So proptastic looks at the first 100 random draws, and says so when one of these is true, before the rest of the tries are spent.

A filter that keeps few values makes the run draw many to keep a few. When over 3 in 4 tries are discarded, by .filter(…) or an assume, or a filter rejects over 3 in 4 of the values it draws, the run fails:

HealthTest.java
@Test
void aRoundLotIsAWholeNumberOfHundreds() {
forAll(roundLotsByFilter()).check(lot -> assertEquals(0, lot % 100));
}
aRoundLotIsAWholeNumberOfHundreds failed a health check (seed 0x5eed)
filter:
filters rejected 1,628 of the 1,728 values they drew in the first 100 tries, over 3 in 4
rerun:
PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'HealthTest.aRoundLotIsAWholeNumberOfHundreds'
! build values that meet the filter, in place of discarding the rest
! or say why it's fine: .allow("filter", "…")

Build the values you want instead of throwing the rest away. Here, a number of hundreds made with .map(n -> n * 100) draws only round lots, and shrinks better too.

An input of more than 10,000 choices, a long string, a list of lists, takes long to draw, check and shrink. When over 1 in 4 of the first draws is that large, the run fails:

HealthTest.java
@Test
void aDocumentSurvivesARoundTrip() {
forAll(strings().ofLength(atLeast(11_000)))
.check(text -> assertEquals(text, Document.parse(Document.format(text))));
}
aDocumentSurvivesARoundTrip failed a health check (seed 0x5eed)
large: 100 of the first 100 draws took over 10,000 choices each, over 1 in 4
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'HealthTest.aDocumentSurvivesARoundTrip'
! bound the sizes: .ofSize(atMost(…)), .ofLength(atMost(…)) or averageSize(…)
! or say why it's fine: .allow("large", "…")

Bound the sizes with .ofSize(atMost(…)), .ofLength(atMost(…)), or .averageSize(…). A bug seldom needs a large input, and a small one is easier to read when it fails.

When drawing the first 100 tries’ values takes over a second, not counting the checks, the run prints a warning naming the property and how long drawing took, and goes on. It’s only a warning: the same draws take longer on a busier machine, and a failure that comes and goes would be worse than none. Bound the sizes, as for large.

Sometimes a check’s cost is one you’ve chosen. Say so, with the reason, and that check isn’t made for the property:

HealthTest.java
@Test
void aRoundLotByFilterIsAWholeNumberOfHundreds() {
forAll(roundLotsByFilter())
.allow("filter", "this page shows the filter's cost")
.check(lot -> assertEquals(0, lot % 100));
}

.allow(…) takes filter, large or slow, and refuses any other name. The reason is for the next person to read the test.

A health check is made once, on the first draws, and the same seed draws the same inputs, so a run that fails one fails it every time with that seed.

Privacy