Quickstart
A test that checks one example passes whenever the code is right for that example. This page has
you add a property beside one, watch it find an input the example missed, and keep that input as
a test that runs every time. It takes about five minutes, on sample-frameworks/quickstart, a
small project in proptastic’s repository, which gets proptastic from Maven Central
(Install adds it to your own build). In a clone of proptastic:
cd sample-frameworks/quickstart1. Write a property
Section titled “1. Write a property”Sort.java sorts a list of numbers:
import java.util.ArrayList;import java.util.List;import java.util.TreeSet;
class Sort { /** The numbers, smallest first. */ static List<Integer> sort(List<Integer> numbers) { return new ArrayList<>(new TreeSet<>(numbers)); }}Its test checks one example, and the example passes. Beside it is a property:
import static io.proptastic.Proptastic.forAll;import static io.proptastic.Proptastic.integers;import static io.proptastic.Proptastic.lists;import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;import org.junit.jupiter.api.Test;
class SortTest { @Test void sortsThreeNumbers() { assertEquals(List.of(1, 2, 3), Sort.sort(List.of(3, 1, 2))); }
@Test void sortKeepsSize() { var ints = lists(integers()); forAll(ints).check(xs -> assertEquals(xs.size(), Sort.sort(xs).size())); }}import static io.proptastic.Proptastic.forAll;import static io.proptastic.Proptastic.integers;import static io.proptastic.Proptastic.lists;import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;import org.junit.jupiter.api.Test;
class SortTest { @Test void sortsThreeNumbers() { assertThat(Sort.sort(List.of(3, 1, 2))).containsExactly(1, 2, 3); }
@Test void sortKeepsSize() { var ints = lists(integers()); forAll(ints).check(xs -> assertThat(Sort.sort(xs)).hasSize(xs.size())); }}lists(integers()) says which inputs to try: lists of integers, named ints. .check(…) says
what must hold for every one of them: sorting a list keeps its size. The property is a plain JUnit @Test,
and the lambda’s parameter, xs, is the name a failure gives the input. Assert as your tests
already do: the AssertJ tabs show each step with AssertJ, which the project has as a test
dependency.
2. Run it
Section titled “2. Run it”./gradlew testSortTest > sortKeepsSize() FAILED io.proptastic.core.PropertyFailure: expected: <2> but was: <1>
sortKeepsSize failed after 1 try (seed 0x5eed) shrunk counterexample: xs = [0, 0] pin it: .example(List.of(0, 0)) rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'SortTest.sortKeepsSize'
Caused by: org.opentest4j.AssertionFailedError: expected: <2> but was: <1>SortTest > sortKeepsSize() FAILED io.proptastic.core.PropertyFailure: Expected size: 2 but was: 1 in: [0]
sortKeepsSize failed after 1 try (seed 0x5eed) shrunk counterexample: xs = [0, 0] pin it: .example(List.of(0, 0)) rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'SortTest.sortKeepsSize'
Caused by: java.lang.AssertionError: Expected size: 2 but was: 1 in: [0]./mvnw test[ERROR] Failures:[ERROR] SortTest.sortKeepsSize:19 expected: <2> but was: <1>
sortKeepsSize failed after 1 try (seed 0x5eed) shrunk counterexample: xs = [0, 0] pin it: .example(List.of(0, 0)) rerun: PROPTASTIC_SEED=0x5eed ./mvnw test -Dtest='SortTest#sortKeepsSize'[ERROR] Failures:[ERROR] SortTest.sortKeepsSize:22 Expected size: 2 but was: 1 in:[0]
sortKeepsSize failed after 1 try (seed 0x5eed) shrunk counterexample: xs = [0, 0] pin it: .example(List.of(0, 0)) rerun: PROPTASTIC_SEED=0x5eed ./mvnw test -Dtest='SortTest#sortKeepsSize'The example passes and the property fails. Each run draws new inputs, so yours may fail after a
different number of tries, with its own seed. This one is the run with PROPTASTIC_SEED=0x5eed.
3. Read it
Section titled “3. Read it”- Your assertion’s message comes first: a list of 2 came back with 1 in it.
- The header names the property, says how many tries it took to fail, and gives the seed, which decides what’s drawn.
- The shrunk counterexample is the smallest input that still fails. proptastic found a failing
list among random ones, then made it smaller until it couldn’t.
[0, 0]has a number twice, and the sort goes through aTreeSet, which keeps each number once. The example had no repeats, so it couldn’t see that. pin itis a line to paste into the property, so this input runs every time.rerunruns this one property again with the same seed, so it draws the same inputs.
4. Pin it
Section titled “4. Pin it”Paste the pin it line after forAll(…):
@Testvoid sortKeepsSize() { var ints = lists(integers()); forAll(ints) .example(List.of(0, 0)) // the input proptastic found .check(xs -> assertEquals(xs.size(), Sort.sort(xs).size()));}@Testvoid sortKeepsSize() { var ints = lists(integers()); forAll(ints) .example(List.of(0, 0)) // the input proptastic found .check(xs -> assertThat(Sort.sort(xs)).hasSize(xs.size()));}Run the tests again:
SortTest > sortKeepsSize() FAILED io.proptastic.core.PropertyFailure: expected: <2> but was: <1>
sortKeepsSize failed on example 1 of 1 counterexample: xs = [0, 0]
Caused by: org.opentest4j.AssertionFailedError: expected: <2> but was: <1>SortTest > sortKeepsSize() FAILED io.proptastic.core.PropertyFailure: Expected size: 2 but was: 1 in: [0]
sortKeepsSize failed on example 1 of 1 counterexample: xs = [0, 0]
Caused by: java.lang.AssertionError: Expected size: 2 but was: 1 in: [0][ERROR] Failures:[ERROR] SortTest.sortKeepsSize:23 expected: <2> but was: <1>
sortKeepsSize failed on example 1 of 1 counterexample: xs = [0, 0][ERROR] Failures:[ERROR] SortTest.sortKeepsSize:23 Expected size: 2 but was: 1 in:[0]
sortKeepsSize failed on example 1 of 1 counterexample: xs = [0, 0]It fails on the example, before any random input is drawn. An example runs first, every time, whatever the seed, and it isn’t shrunk: it’s already the input you chose. So there’s no seed to copy and nothing to pin. The bug is now a test.
5. Fix it
Section titled “5. Fix it”Sort the list without a set, so a number that’s there twice stays twice:
import java.util.List;
class Sort { /** The numbers, smallest first. */ static List<Integer> sort(List<Integer> numbers) { return numbers.stream().sorted().toList(); }}Run the tests again, and both pass. Keep the example: if a later change brings the bug back, it
fails on [0, 0] first, whatever the random inputs are.
With the proptastic command
Section titled “With the proptastic command”./gradlew test runs every test, and a setting such as a new seed doesn’t make Gradle run an
unchanged task again. The proptastic command runs your properties through your build all the
same, only them once it knows which they are, and says what each found. On this project, before
the fix:
proptastic run Failed ────────────────────────────────────────────────────────────────
✗ SortTest#sortKeepsSize counterexample ca97c5d54 expected: <2> but was: <1>
sortKeepsSize failed after 1 try (seed 0x2fc03457e2e24c2) shrunk counterexample: xs = [0, 0] pin it: .example(List.of(0, 0)) rerun: proptastic run --only='SortTest#sortKeepsSize()' --seed=0x2fc03457e2e24c2
Next: proptastic run --only='SortTest#sortKeepsSize()' --seed=0x2fc03457e2e24c2
Properties ✗ 1 failed (1) Ran ./gradlew test --rerun Reports build/proptastic/report/report.json · build/proptastic/report/index.html
registered 1 property; later runs run only thoseIts Next: line runs that property alone, from the same seed, and proptastic example pins its
counterexample for you: Keeping an example.
proptastic run has every option.
Next: check it against a model
Section titled “Next: check it against a model”A property says one true thing about your code. A model says everything the code should do, in a few lines written to be obviously right. proptastic runs the model beside your code and stops at the first step where they disagree, shrunk to the shortest run that shows it. Follow a model does this for a counter, with nothing to install.
- Cheatsheet: what you’ve just used and the rest of the API on one page, to keep open while you write properties, or print.
- Test first: a worked example: write the property before the code, and let each counterexample say what to build next.
- With your test framework: the same property in JUnit 4, TestNG, Kotlin and others, and how each shows a failure.
- Domains: the inputs you can draw, from numbers to your own records.
- A bank account: one property built up in steps, until it checks a rule after every operation.
- Shrinking and Seeds and replay: how proptastic gets from a
random failure to
[0, 0], and how a seed draws it again.