Skip to content
Engineer
Menu
Design
English

Engineer / Glossary

Property-based testing

testing a rule across generated inputs

What it is

Property-based testing checks a statement about a program over many generated inputs. A property expresses something the result should always satisfy within a defined domain. A parser might be expected to read every valid file the program writes. A normalisation function might be expected to produce the same result when run a second time. The test supplies examples and looks for one that makes the statement false.

The generator describes the kinds of inputs the test will explore. It can create strings, structured records, nested objects or a sequence of operations. Good generators reach important boundaries such as empty collections, duplicated identifiers and values close to supported limits. They can express preconditions so that the resulting cases are meaningful to the property.

When a tool finds a failure, it may shrink the input to a smaller example that still violates the property. That can turn a large generated record into a small combination of fields or an unexpected character. Retaining the failing example or the reproducible test state helps a maintainer verify the fix. The small example documents a real failure. The property can keep exploring other cases after the defect is corrected.

Why it matters

A familiar example test proves what happens for one input selected in advance. Its value depends on how accurately the expected result represents the rule. For a parser or data transformation, people cannot easily list every meaningful combination. An assertion over a family of inputs can reveal differences between a design’s rule and the examples its authors thought to write down.

The quality of the property is as important as the size of the generated sample. Suppose a function takes records and returns them sorted. A check that only compares the first and last values could pass when the middle is in the wrong order. A useful property can require the whole result to be ordered and retain the same records in the expected multiplicities. It can also specify what to do when several records share a sort key.

Generators can expose variation people commonly omit. A registration flow can receive names that contain combining characters. A normalisation function can see valid values with different decimal scales. A data update can be delivered twice. When those differences affect the system’s contract, make them part of the generator and specify the result before looking at failures. Random variation alone supplies little evidence about which parts of the domain were tested.

What it looks like in practice

Imagine an encoder and decoder for a structured customer export. Start by defining what it means for the records before encoding and after decoding to be equivalent. The export may preserve exact identifiers and deliberately normalise whitespace in a description. Generate supported records, encode each one and check that decoding returns the expected representation. Include absent optional fields, multiple records with the same display name and text near size boundaries.

Define a second property for repeated normalisation. Run the normaliser once, run it again on its own output and compare the results under the chosen equality rule. If a generated input fails, retain a minimal example and locate the stage that changed. Reproduce it before changing the implementation. Add a focused regression case where it makes the expected behaviour easier to read, then keep the wider property and its generator.

Engineer’s repeatable database generator case study records an end-to-end rule that a second run over the first result must preserve identifiers, row counts and contents. That is a concrete invariant even when exercised with a particular fixture. A property-based test could develop the same reasoning with several generated valid starting datasets. The expected invariance needs to come from the pipeline’s intended use.

Where it goes wrong

A generator can spend most of its time producing inputs that never reach the behaviour under test. If an example discards almost every record because it lacks a permitted field, the property may mostly measure rejection. Construct valid inputs directly where the decision concerns meaningful records. Add a separate property for deliberate validation failures, where rejected inputs are the point.

A property can also compare the implementation with itself. If the expected result calls the same transformation being tested, a shared mistake can appear on both sides. State the invariant directly, use a small independent calculation, or compare with another specification of the contract. Watch for legitimate choices such as ordering of equivalent data and representation of numbers. Give them a clear equality rule that a reader can verify.

Tests of operations that consult external state need additional care. If a property calls a service whose data changes during the run, a minimal failing input may stop reproducing when the tool retries it. Keep the tested function deterministic where possible, or make the relevant external state part of the fixture. Assert explicit state transitions in a sequence-based property, including what a second operation should see after the first one succeeds.

Consider recording meaningful regions of the input space. A sequence generator might consistently produce short cases and rarely exercise a later retry or empty result. Partitioning the input by expected categories, and observing their frequency, helps show whether the available runtime covers the decisions that matter.

Finally, retain feedback about reach and reproducibility. Record the test seed, the generated categories worth observing and the minimal input when a failure is found. Give each run a cost that fits where it is used. A short check can run routinely, with broader settings for a scheduled job. A well-designed property strengthens the suite because it names behaviour the team can explain and produces concrete examples when the program disagrees.

Further reading

Contact

Still working out what you need?

Tell us what is going wrong, or what you are trying to build. We will tell you what it would take, roughly what it would cost, and whether we are the right people for it.

From 1,400 DKK an hour, excluding VAT — and less per hour on a longer booking.

welcome@engineer.company +45 50 10 61 84 Copenhagen, Denmark