Skip to content

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.

SortTest.java
import static io.proptastic.Proptastic.*;
import static io.proptastic.bounds.Bounds.*;
@Test
void 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.

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 with domain(source -> …); any(Trade.class) draws by type. .flatMap only when one value decides another’s domain.
  • Every domain, and what it draws: the catalogue.
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

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

@Test
void bySetKeepsSize() {
forAll(lists(integers())) // sorts through a set
.check(xs -> assertThat(bySet(xs)).hasSize(xs.size()));
}
Terminal window
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

  • Pin with the pin it line, or proptastic 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 over PROPTASTIC_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

@StepBuilder
interface 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);
}
@Test
void theBankKeepsItsBooks() {
forAll(built()).check();
}
Terminal window
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();
static Steps<Counter> aCounter() {
return steps(() -> new Counter(lock("counter")))
.step("next", counter -> record(counter.next()));
}
@Test
void 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.

Terminal window
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

Terminal window
proptastic run # every property
proptastic run --only=SortTest#sortKeepsSize
proptastic run --seed=0x5eed # the same tries again
proptastic run --replay # no new tries
proptastic run --profile=nightly # search harder
proptastic explain sortKeepsSize # all that's known
proptastic example # what's waiting
proptastic example c4f1a2b9e # pin it in the code
proptastic example c4f1a2b9e --propose --reason="empty carts"
proptastic example c4f1a2b9e --accept # a person accepts
proptastic fuzz --for=30m # search for longer
proptastic corpus --merge=ci-corpus # take in CI's corpus

Exit 0 is a pass, 1 a counterexample. proptastic run, exit codes

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

Environment variables, or system properties: PROPTASTIC_TRIES is proptastic.tries.

SettingDefaultWhat it is
PROPTASTIC_SEEDa new seed each runThe seed every random try is drawn from, so a run can be repeated.
PROPTASTIC_TRIESthe property's .tries(…), else 100How many tries each property makes.
PROPTASTIC_PROFILElocalHow much a run spends.
PROPTASTIC_BUDGETnoneThe most time a property may spend trying.
PROPTASTIC_SHRINKonWhether a failure is shrunk.
PROPTASTIC_REPLAYonWhether the counterexample a property's last run saved is tried first.
PROPTASTIC_STATISTICSoffWhether a passing property prints what classify counted.
PROPTASTIC_AGENToffPrints 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.

Every setting

Quickstart · Install · Domains · Shrinking · How sure is a pass · Follow a model · Fuzzing · Patterns · Test frameworks · CI · API · Vocabulary

Privacy