Cheatsheet
What you reach for most while writing properties, on one page to keep open beside your editor, or to print and pin up. Each part links to its full page.
A property
Section titled “A property”import static io.proptastic.Proptastic.*;import static io.proptastic.bounds.Bounds.*;
@Testvoid sortKeepsSize() { forAll(lists(integers())) // which inputs .check(xs -> assertThat(sort(xs)).hasSameSizeAs(xs));}forAll(…) says which inputs, .check(…) what must hold for each, in a plain @Test.
Properties
forAll(lists(integers())) .example(List.of(0, 0)) // runs first, every run .tries(1_000) // random tries after the examples .seed(0x5eed) // the same tries every run .check(xs -> assertThat(sort(xs)).hasSameSizeAs(xs));forAll(integers(), integers()) // two draws, or more .example(0, 1) // a value for each .check((one, two) -> assertEquals(one + two, two + one));.exhaustively() checks every value when there are few enough; .id("…") keeps the
corpus through a rename.
Domains
Section titled “Domains”var counts = longs().atLeast(1).atMost(1_000);var prices = doubles().finite().atLeast(0);var codes = strings().ofLength(3).from(UPPERCASE);var sides = elements("buy", "sell");var baskets = lists(counts).ofSize(atLeast(1));var tags = sets(codes);var stock = maps(codes, counts);var notes = optionals(strings());var nonZero = integers().excluding(0);var evens = integers().map(half -> half * 2);var small = integers().filter(number -> number < 100);var mixed = oneOf(weighted(3, evens), nonZero);var trades = domain(codes, counts, Trade::new);var typed = any(Trade.class).with(Trade::quantity, counts);var lengths = integers().atLeast(0).atMost(5);var sized = lengths.flatMap(size -> lists(sides).ofSize(size));- Bounds first, then
.excluding(…)or.edges(…). Prefer a bound to.filter, which throws draws away. - Combine with
domain(…, Trade::new), or field by field withdomain(source -> …);any(Trade.class)draws by type..flatMaponly when one value decides another’s domain. - Every domain, and what it draws: the catalogue.
More domains
Section titled “More domains”var days = dates().atLeast(LocalDate.of(2026, 1, 1));var weekdays = dates().excluding(SATURDAY, SUNDAY);var slots = times().atLeast(LocalTime.NOON).every(ofMinutes(15));var moments = instants();var waits = durations().atLeast(Duration.ZERO);var zoned = dateTimes().inZones(zones());var cells = dates().inSerials(Serial.EXCEL_1900);Dates aim a share of their tries at the calendar’s edges, such as 29 February and month ends;
inZones is provisional. Dates and times
var prices = decimals().places(2).atLeast(BigDecimal.ZERO);var huge = bigIntegers();var ids = uuids();var cards = luhnNumbers();var accounts = ibans();var typos = isbns().mistyped();var skus = strings().matching("[A-Z]{3}-[0-9]{4}");var payloads = bytes().ofSize(atMost(64));var encoded = bytes().inBase64();Identifiers have valid check digits; .mistyped() has one digit wrong. matching draws text
for a regular expression. Formats
Inside the check
Section titled “Inside the check”forAll(integers(), integers()) .check( (num, den) -> { assume(den != 0); // not a try: draw again classify(num == 0, "zero"); // counted require(10, num < 0, "negative"); // or fail assertThat(num / den * den + num % den).isEqualTo(num); });target(score) steers the tries toward a higher score.
Classify and require, Targeted tries
A failure
Section titled “A failure”@Testvoid bySetKeepsSize() { forAll(lists(integers())) // sorts through a set .check(xs -> assertThat(bySet(xs)).hasSize(xs.size()));}Expected size: 2 but was: 1 in:[0]
bySetKeepsSize failed after 2 tries (seed 0x5eed) shrunk counterexample: xs = [0, 0] pin it: .example(List.of(0, 0)) rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'CheatsheetTest.bySetKeepsSize'Your assertion’s message, the header, then the smallest input that still fails (fails for any names values that played no part), pin it to paste after forAll(…), and rerun
from the same seed. Reading a failure
Pins, seeds and replay
Section titled “Pins, seeds and replay”- Pin with the
pin itline, orproptastic example ID. A value with no source form pins its seed;.writtenAs(…)on its domain fixes that. - A seed draws the same inputs anywhere.
.seed(0x…)wins overPROPTASTIC_SEED. - A failure is saved and tried first next run, until it passes.
- The order: examples, the saved counterexample, the corpus, then random tries.
Seeds and replay, Keeping an example
Steps from an interface; a @Handle is a value one step makes and others draw. By hand,
steps(…).step("name", domains…, action): Stateful testing
@StepBuilderinterface Banking { @Handle("account") long open();
void deposit(@Handle("account") long account, long amount);
void withdraw(@Handle("account") long account, long amount);}static Steps<Bank> built() { return BankingSteps.steps(Bank::new) .open() .deposit(amounts()) .withdraw(amounts()) .invariant("never below zero", Bank::noneBelowZero);}
@Testvoid theBankKeepsItsBooks() { forAll(built()).check();}invariant "never below zero" broke after step 2: withdraw(account1, 1)
theBankKeepsItsBooks failed after 1 try (seed 0x5eed) shrunk steps: 1. var account1 = open() 2. withdraw(account1, 1) ← account 1 holds -1 pin it: .example(open(), withdraw(account(1), 1L)) // import static cheatsheet.BankingStep.* rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'StepsTest.theBankKeepsItsBooks'Paste the pin; a handle pins as account(1):
forAll(built()) .example(open(), withdraw(account(1), 1L)) // pasted .check();Threads
Section titled “Threads”static Steps<Counter> aCounter() { return steps(() -> new Counter(lock("counter"))) .step("next", counter -> record(counter.next()));}
@Testvoid eachNextIsNew() { forAll(aCounter()).threads(2).check();}.threads(2) runs the steps on two threads, every schedule with a few preemptions. What each
record(…)s must fit some order of the steps; lock(…) is a lock it switches at.
no order of the threads' steps, one at a time, gives what they saw they saw: thread 1: next() → 1 thread 2: next() → 1 one at a time: next() → 1, then next() → 2 thread 1's next() → 1 began before thread 2's next() and ended after it
eachNextIsNew failed after 4 tries (seed 0x5eed) shrunk steps: thread 1 thread 2 1. next() locks and unlocks "counter" paused before it takes "counter" 1. next() locks and unlocks "counter" locks and unlocks → 1 locks and unlocks → 1 pin it: .example(thread(step("next")), thread(step("next"))) // typed pins come from @StepBuilder rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'StepsTest.eachNextIsNew'Pin with .example(first(…), thread(…), thread(…)); to switch at plain fields, add the
agent jar. Concurrency
The command line
Section titled “The command line”proptastic run # every propertyproptastic run --only=SortTest#sortKeepsSizeproptastic run --seed=0x5eed # the same tries againproptastic run --replay # no new triesproptastic run --profile=nightly # search harderproptastic explain sortKeepsSize # all that's knownproptastic example # what's waitingproptastic example c4f1a2b9e # pin it in the codeproptastic example c4f1a2b9e --propose --reason="empty carts"proptastic example c4f1a2b9e --accept # a person acceptsproptastic fuzz --for=30m # search for longerproptastic corpus --merge=ci-corpus # take in CI's corpusExit 0 is a pass, 1 a counterexample. proptastic run,
exit codes
Coding agents
Section titled “Coding agents”PROPTASTIC_AGENT=1, or proptastic run --format=agent: the verdict and rules first, the report
fenced as data. For a property it didn’t write, an agent proposes with --propose, and a person accepts.
Coding agents
Settings
Section titled “Settings”Environment variables, or system properties: PROPTASTIC_TRIES is proptastic.tries.
| Setting | Default | What it is |
|---|---|---|
PROPTASTIC_SEED | a new seed each run | The seed every random try is drawn from, so a run can be repeated. |
PROPTASTIC_TRIES | the property's .tries(…), else 100 | How many tries each property makes. |
PROPTASTIC_PROFILE | local | How much a run spends. |
PROPTASTIC_BUDGET | none | The most time a property may spend trying. |
PROPTASTIC_SHRINK | on | Whether a failure is shrunk. |
PROPTASTIC_REPLAY | on | Whether the counterexample a property's last run saved is tried first. |
PROPTASTIC_STATISTICS | off | Whether a passing property prints what classify counted. |
PROPTASTIC_AGENT | off | Prints a failure for a coding agent to read: the verdict and the rules first, then the report inside a fence that marks it as data. |
The full pages
Section titled “The full pages”Quickstart · Install · Domains · Shrinking · How sure is a pass · Follow a model · Fuzzing · Patterns · Test frameworks · CI · API · Vocabulary