Skip to content

Test first: a worked example

Paging a list looks like one line of code, and it’s easy to get wrong in three different ways. Written test first with examples, you’d find each when you thought of the case, or when a user did. Written property first, each one turns up as the next failing input, made as small as it goes, so it says exactly what to build.

This page builds it a cycle at a time, with every failure exactly as proptastic prints it: a property, red, the counterexample pinned, green, and the next property. Test first with properties says why the loop goes this way.

The spec: a list shows a number of items to a page. How many pages does it need?

The rule, said for every list: every item has a place. The pages, times the items each holds, are at least the items. The property says that, for any number of items and any page size of at least one:

@Test
void everyItemHasAPlace() {
forAll(integers().atLeast(0), integers().atLeast(1))
.check(
(items, perPage) -> {
int pages = Pages.count(items, perPage);
long places = (long) pages * perPage;
assertThat(places).isGreaterThanOrEqualTo(items);
});
}

And just enough code for it to compile:

/** How many pages a list of items needs, a number of items to a page. */
final class Pages {
static int count(int items, int perPage) {
return 0;
}
}

Run it, and it fails:

Terminal window
Expecting actual:
0L
to be greater than or equal to:
1L
everyItemHasAPlace failed after 1 try (seed 0x5eed)
shrunk counterexample: items = 1, perPage = 1
fails for any: items, perPage
pin it: .example(/* items */ 1, /* perPage */ 1)
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'PagesTest.everyItemHasAPlace'

The first input it drew failed, and proptastic shrank it to the smallest that fails the same way: 1 item, 1 to a page. That’s the smallest thing the code gets wrong, and so the next thing to build: a list with something in it needs a page. fails for any says it would fail whatever either value was (Which values mattered).

Paste the pin it line after forAll(…), before you write any code:

@Test
void everyItemHasAPlace() {
forAll(integers().atLeast(0), integers().atLeast(1))
.example(/* items */ 1, /* perPage */ 1)
.check(
(items, perPage) -> {
int pages = Pages.count(items, perPage);
long places = (long) pages * perPage;
assertThat(places).isGreaterThanOrEqualTo(items);
});
}

The example runs first, every time, before any random try, whatever the seed. The test stays red on that input until the code handles it:

Terminal window
Expecting actual:
0L
to be greater than or equal to:
1L
everyItemHasAPlace failed on example 1 of 1
counterexample: items = 1, perPage = 1

The least code that passes. With one example test, that would be a constant, return 1, and it would take a second example to force anything more. The property is that second example, and the third: return 1 passes the pinned one, then meets the inputs the property draws.

Terminal window
Expecting actual:
1L
to be greater than or equal to:
2L
everyItemHasAPlace failed after 2 tries (seed 0x5eed)
shrunk counterexample: items = 2, perPage = 1
fails for any: items
pin it: .example(/* items */ 2, /* perPage */ 1)
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'PagesTest.everyItemHasAPlace'

Pin that too, .example(/* items */ 2, /* perPage */ 1), under the first. One page an item passes every input:

/** How many pages a list of items needs, a number of items to a page. */
final class Pages {
static int count(int items, int perPage) {
return items;
}
}

The property passes. And the code is plainly wrong: 10 items, 10 to a page, is 10 pages. The rule holds, so the rule says too little. That’s the cue for the next property.

What the code is still free to do is give too many pages. So: the last page is never empty. The pages before it hold fewer than all the items:

@Test
void theLastPageIsNeverEmpty() {
forAll(integers().atLeast(0), integers().atLeast(1))
.check(
(items, perPage) -> {
int pages = Pages.count(items, perPage);
long beforeTheLast = (long) (pages - 1) * perPage;
assertThat(beforeTheLast).isLessThan(items);
});
}
Terminal window
Expecting actual:
2L
to be less than:
2L
theLastPageIsNeverEmpty failed after 1 try (seed 0x5eed)
shrunk counterexample: items = 2, perPage = 2
fails for any: items, perPage
pin it: .example(/* items */ 2, /* perPage */ 2)
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'PagesTest.theLastPageIsNeverEmpty'

2 items, 2 to a page, needs one page, not two. Pin it in this property, .example(/* items */ 2, /* perPage */ 2), and on to green.

The least code for this one is a division:

/** How many pages a list of items needs, a number of items to a page. */
final class Pages {
static int count(int items, int perPage) {
return items / perPage;
}
}

The new property passes, and the first one, which still runs, fails:

Terminal window
Expecting actual:
0L
to be greater than or equal to:
1L
everyItemHasAPlace failed after 1 try (seed 0x5eed)
shrunk counterexample: items = 1, perPage = 2
fails for any: perPage
pin it: .example(/* items */ 1, /* perPage */ 2)
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'PagesTest.everyItemHasAPlace'

A division in ints rounds down, and 1 item, 2 to a page, needs a page, not none. The examples pinned so far are 1 to a page, which divides exactly, so they couldn’t see this; the property drew a case that could. Pin it beside them, and round up instead: add a page less one item, then divide.

/** How many pages a list of items needs, a number of items to a page. */
final class Pages {
static int count(int items, int perPage) {
return (items + perPage - 1) / perPage;
}
}
Terminal window
Expecting actual:
-2147483647L
to be greater than or equal to:
2L
everyItemHasAPlace failed after 14 tries (seed 0x5eed)
shrunk counterexample: items = 2, perPage = 2147483647
fails for any: items
pin it: .example(/* items */ 2, /* perPage */ 2147483647)
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'PagesTest.everyItemHasAPlace'

Every pinned example passes now, and 14 tries in, a random one didn’t. With pages as big as an int goes, items + perPage - 1 passes the largest int and wraps round to a negative number. A domain leans toward its edges, the largest number among them (How values are drawn), which is how it got there. Pin it too.

Divide, and add a page for what’s left over, so nothing is added before the division:

/** How many pages a list of items needs, a number of items to a page. */
final class Pages {
static int count(int items, int perPage) {
return items / perPage + (items % perPage == 0 ? 0 : 1);
}
}

Both properties pass, every pinned example first. Here’s the first property, with the four inputs the code once got wrong:

@Test
void everyItemHasAPlace() {
forAll(integers().atLeast(0), integers().atLeast(1))
.example(/* items */ 1, /* perPage */ 1)
.example(/* items */ 2, /* perPage */ 1)
.example(/* items */ 1, /* perPage */ 2)
.example(/* items */ 2, /* perPage */ 2147483647)
.check(
(items, perPage) -> {
int pages = Pages.count(items, perPage);
long places = (long) pages * perPage;
assertThat(places).isGreaterThanOrEqualTo(items);
});
}

Commit. The pins are the example tests you’d have written by hand, but you didn’t choose them: each is a case the code got wrong, made as small as it goes, and checked on every run from now on.

The properties say the count is right for every list. They don’t say how a page reads. “Page 1 of 3” is exact text, so it’s an example test: Test first with properties says when an example is the better first step.

Privacy