Skip to content

Test first with properties

Working test first, you write a test, watch it fail, then write the code that makes it pass. With example tests, you also choose every input, so each next step is one you thought of. The case you didn’t think of, a list that fills its last page exactly, a number at the top of its range, waits until someone meets it in production. A property written first draws the inputs for you, and when it fails, it says which one, made as small as it goes.

This page says how the loop works with properties, and where an example is still the better first step. Test first: a worked example runs it, with every failure as it prints.

Start from what the code must do, said as a rule for every input: every item has a place on some page; the last page is never empty. That’s the property. Write it, and a stub of the code so it compiles, before any of the code itself.

A rule is often easier to say than a list of cases, and it’s the same rule you’d check the code against in review. Patterns has the rules that come up most: a round trip, an invariant, a comparison with a simpler version you trust.

Red: the counterexample says what to build

Section titled “Red: the counterexample says what to build”

Run it, and it fails. proptastic doesn’t stop at the first input that fails: it shrinks it to the smallest that still fails the same way. So the counterexample isn’t just some input the code gets wrong. It’s the simplest thing the code gets wrong, and that’s exactly what to build next.

A stub that says every list needs no pages fails on 1 item, 1 to a page. A fix that rounds down fails on 1 item, 2 to a page. A fix that rounds up by adding first fails on pages as big as an int goes. Each failure names the next step, smallest case first, without you choosing it.

Write the least code that makes the property pass. With examples, the least code can be a constant that passes the one case you wrote, and it takes another example to force the general code. A property gives you that second case at once: a constant fails on the next input drawn. The property does the triangulating for you.

The least code can still be wrong. One page an item passes “every item has a place”, and it’s nothing like right. That’s when you write the next property, the rule the code is still free to break: here, that the last page is never empty.

When a property fails, the failure ends with a pin it line, such as .example(/* items */ 1, /* perPage */ 1). Paste it after forAll(…) before you write the fix. The example runs first, every time, before any random try, whatever the seed. So the test stays red on that case until the code handles it, and afterwards it’s checked on every run, on every machine, for good. A saved counterexample is tried first next run too, but it lives in the build directory, and a clean deletes it (Counterexamples and ids). A pin is in the test.

Over a few cycles the pins add up to the example list you’d have written by hand, except each one is a case the code once got wrong. proptastic example pins one for you: Keeping an example.

Step What you do What proptastic does
Property Write the rule, and a stub so it compiles
Red Run it: it must fail Shrinks the failing input, and prints it with its pin it line
Pin Paste the pin it line Runs the example first, every time
Green The least code that passes, with every property green Draws new inputs each run, so a shortcut meets the next case
Next The rule the code is still free to break
Refactor Change the structure, with every property green The same rules hold for the new shape: Refactoring under properties

Commit at the end of the cycle. A run draws new inputs each time, so a property that passed can fail later on code nobody changed: that’s an input it hadn’t drawn before, not a flaky test. The failure’s rerun line draws it again (Seeds and replay).

  • The rule is clear and the cases aren’t. Paging, splitting a bill, sorting, a parser and its printer: you can say what must hold more easily than list the inputs where it’s tricky.
  • The edges are the risk. Zero, one, the largest number, an empty list, a value twice. A domain leans toward them (How values are drawn), so the first runs try them without you choosing.
  • You’d fake it otherwise. A property can’t be passed by a constant, so green has to be the general code, or close to it.
  • Something to compare with exists. An old version, a slow but obvious one, a model: the property is “the new code agrees with it”, and you write no expected values at all.
  • You don’t know the rule yet. One case worked by hand, “3 items, 2 to a page, is 2 pages”, is often how you find it. Write it as an example test, or as the first .example(…) of a property whose check you write next.
  • The output is exact. A label, “Page 1 of 3”, or a format a file depends on. A property can say the label names a page that exists; only an example says it reads that way. Keep both (Migrating from example tests).
  • The case is the rule. “An empty list needs no pages” is one input. Pin it on the property that covers it, or keep it as an example test.
  • A property passes code that’s plainly wrong. That’s not a reason to drop it: it’s a sign the rule says too little, and the next property, or an example, says the rest.

A property checks the rule you wrote, so a rule that’s wrong passes code that’s wrong. Read each property against the spec as you’d read the code. And a pass says the rule held for the inputs drawn (How sure is a pass?); whether your properties would catch a real bug is what mutastic checks.

Privacy