Property testing, the Vary way

A property test does not check an answer. It checks a rule. Instead of asserting that sort([3, 1, 2]) gives [1, 2, 3], you assert that for any list, the result is ordered, has the same length, and holds the same elements. The framework generates the lists, including the empty one and the one full of duplicates that nobody would have typed by hand.

Vary has this built into the test DSL. The keyword is across.

test "concatenation adds lengths" {
    across(a: Str, b: Str) {
        observe len(a + b) == len(a) + len(b)
    }
}

Across the bridge covers the mechanics. This post is about how across fits with mutation testing and agents, and about vocabulary, since the same testing term often means different things in different tools.

A weak property still passes

The weak spot in a property test is the property. A test that says len(sort(xs)) == len(xs) runs 100 generated inputs and passes. A sort that returns its input unchanged passes too. Generating more inputs does not make a weak claim stronger.

Mutation testing catches this. vary mutate makes small changes to your code, like flipping a comparison or swapping an operator, and reruns your tests against each change. If the changed program still passes, the tests did not notice. That is a surviving mutant, and it usually means a property is too loose.

The techniques answer different questions.

TechniqueWhat it answersWhat it cannot tell you
Example testDoes this specific input give this specific output?Anything about inputs you did not write down
across propertyDoes this rule hold over many generated inputs?Whether the rule is strong enough to catch a real bug
vary mutateWould the suite notice if the code were wrong?Whether the code does what the user wanted in the first place
Contracts and proofsDoes this hold for every call, or provably for all inputs in scope?Behaviour the contract does not mention

Each mutant is run against every generated input in every across block, so it has to get past far more than three hand-picked examples to survive.

Why this suits agents

Property tests and mutation runs are tedious to write by hand, and an agent can write them in volume. It can read a function, propose the laws the function should obey, write the across blocks, run vary mutate, and treat the survivors as a to-do list. Each survivor names a specific change the suite missed, so the next step is concrete: tighten a property or add a case.

The payoff comes later. When an agent or a person edits the code next month, the generated inputs and the mutation score catch the regression, and nobody has to remember what the old behaviour was.

There is one trap. An agent that derives its properties from the implementation will encode the implementation's bugs as the specification. Properties should come from what the code is supposed to do: the requirements, the contract, the documented behaviour. Survivors help here too, because properties copied from the code tend to be the ones that let mutants through.

One map of testing names

QuickCheck, Hypothesis, PIT, and Vary each use slightly different words for the same ideas. This table maps the common terms to Vary's.

Common termIn VaryNotes
Property testacross(...) { observe ... } inside a test blockA test that holds over generated inputs
PropertyThe observe lines inside an across blockThe rule that must hold for every input
Assertion, oracleobserveThe point where the test decides pass or fail
Generator, strategyThe declared binding type, as in x: IntValues are generated from the type, no registration step
Case, example, runOne iteration of an across blockCount set with --across-cases, default 100
Seed--across-seedSame seed, same inputs, same failure
CounterexampleThe failing input in the test outputReported with the seed and iteration number
ShrinkingMinimizing a failing input before reportingOn by default, controlled with --across-max-shrink and --no-across-shrink
MutantA small deliberate change to your codeProduced by vary mutate
Killed mutantA mutant that made a test or contract failEvidence the suite notices that kind of fault
Surviving mutantA mutant that still passed every testEvidence of a missing or weak check
Mutation scoreShare of mutants killedMeasures the suite, not the code
Contractin {}, out (result) {}, and invariantsChecked on every call, not only inside tests
Proofvary proveEstablishes a property for all inputs in scope instead of sampling

The glossary has a shorter version of this table.

Try it

Pick a function you trust and write the laws it should obey. Then run the mutation tool to see which changes those laws miss.

vary test .
vary mutate .

Identities, inverses, and ordering rules are good first properties. After that, the survivors show where to go next. For the full reference, see the testing docs and the mutation workflow.