{"canonical_url":"https://engineer.company/glossary/property-based-testing/","content_markdown":"# Property-based testing\n\n## What it is\n\nProperty-based testing checks a statement about a program over many generated\ninputs. A property expresses something the result should always satisfy within a\ndefined domain. A parser might be expected to read every valid file the program\nwrites. A normalisation function might be expected to produce the same result\nwhen run a second time. The test supplies examples and looks for one that makes\nthe statement false.\n\nThe generator describes the kinds of inputs the test will explore. It can create\nstrings, structured records, nested objects or a sequence of operations. Good\ngenerators reach important boundaries such as empty collections, duplicated\nidentifiers and values close to supported limits. They can express preconditions\nso that the resulting cases are meaningful to the property.\n\nWhen a tool finds a failure, it may shrink the input to a smaller example that\nstill violates the property. That can turn a large generated record into a small\ncombination of fields or an unexpected character. Retaining the failing example\nor the reproducible test state helps a maintainer verify the fix. The small\nexample documents a real failure. The property can keep exploring other cases\nafter the defect is corrected.\n\n## Why it matters\n\nA familiar example test proves what happens for one input selected in advance.\nIts value depends on how accurately the expected result represents the rule. For\na parser or data transformation, people cannot easily list every meaningful\ncombination. An assertion over a family of inputs can reveal differences between\na design's rule and the examples its authors thought to write down.\n\nThe quality of the property is as important as the size of the generated sample.\nSuppose a function takes records and returns them sorted. A check that only\ncompares the first and last values could pass when the middle is in the wrong\norder. A useful property can require the whole result to be ordered and retain\nthe same records in the expected multiplicities. It can also specify what to do\nwhen several records share a sort key.\n\nGenerators can expose variation people commonly omit. A registration flow can\nreceive names that contain combining characters. A normalisation function can\nsee valid values with different decimal scales. A data update can be delivered\ntwice. When those differences affect the system's contract, make them part of\nthe generator and specify the result before looking at failures. Random\nvariation alone supplies little evidence about which parts of the domain were\ntested.\n\n## What it looks like in practice\n\nImagine an encoder and decoder for a structured customer export. Start by\ndefining what it means for the records before encoding and after decoding to be\nequivalent. The export may preserve exact identifiers and deliberately normalise\nwhitespace in a description. Generate supported records, encode each one and\ncheck that decoding returns the expected representation. Include absent optional\nfields, multiple records with the same display name and text near size\nboundaries.\n\nDefine a second property for repeated normalisation. Run the normaliser once,\nrun it again on its own output and compare the results under the chosen equality\nrule. If a generated input fails, retain a minimal example and locate the stage\nthat changed. Reproduce it before changing the implementation. Add a focused\nregression case where it makes the expected behaviour easier to read, then keep\nthe wider property and its generator.\n\nEngineer's\n[repeatable database generator case study](/portfolio/held-the-generator-to-981-tests-and-92-coverage/)\nrecords an end-to-end rule that a second run over the first result must preserve\nidentifiers, row counts and contents. That is a concrete invariant even when\nexercised with a particular fixture. A property-based test could develop the\nsame reasoning with several generated valid starting datasets. The expected\ninvariance needs to come from the pipeline's intended use.\n\n## Where it goes wrong\n\nA generator can spend most of its time producing inputs that never reach the\nbehaviour under test. If an example discards almost every record because it\nlacks a permitted field, the property may mostly measure rejection. Construct\nvalid inputs directly where the decision concerns meaningful records. Add a\nseparate property for deliberate validation failures, where rejected inputs are\nthe point.\n\nA property can also compare the implementation with itself. If the expected\nresult calls the same transformation being tested, a shared mistake can appear\non both sides. State the invariant directly, use a small independent\ncalculation, or compare with another specification of the contract. Watch for\nlegitimate choices such as ordering of equivalent data and representation of\nnumbers. Give them a clear equality rule that a reader can verify.\n\nTests of operations that consult external state need additional care. If a\nproperty calls a service whose data changes during the run, a minimal failing\ninput may stop reproducing when the tool retries it. Keep the tested function\ndeterministic where possible, or make the relevant external state part of the\nfixture. Assert explicit state transitions in a sequence-based property,\nincluding what a second operation should see after the first one succeeds.\n\nConsider recording meaningful regions of the input space. A sequence generator\nmight consistently produce short cases and rarely exercise a later retry or\nempty result. Partitioning the input by expected categories, and observing their\nfrequency, helps show whether the available runtime covers the decisions that\nmatter.\n\nFinally, retain feedback about reach and reproducibility. Record the test seed,\nthe generated categories worth observing and the minimal input when a failure is\nfound. Give each run a cost that fits where it is used. A short check can run\nroutinely, with broader settings for a scheduled job. A well-designed property\nstrengthens the suite because it names behaviour the team can explain and\nproduces concrete examples when the program disagrees.\n\n## Further reading\n\n- [The Hypothesis quickstart](https://hypothesis.readthedocs.io/en/latest/quickstart.html).\n  Introduces Python strategies, properties and generated counterexamples.\n- [fast-check's property-based testing introduction](https://fast-check.dev/docs/introduction/what-is-property-based-testing/).\n  Explains the approach for JavaScript and TypeScript.\n- [The QuickCheck package](https://hackage.haskell.org/package/QuickCheck).\n  Documents the original Haskell testing library and its generators.\n\n\u003chttps://engineer.company/glossary/property-based-testing/\u003e\n","date_modified":"2026-11-25T07:18:46Z","description":"Create generated test inputs, shrink a counterexample and keep a reproducible example of the behaviour that failed.","formats":{"gemtext":"https://engineer.company/glossary/property-based-testing/index.gmi","gopher":"https://engineer.company/glossary/property-based-testing/gophermap","html":"https://engineer.company/glossary/property-based-testing/","json":"https://engineer.company/glossary/property-based-testing/index.json","markdown":"https://engineer.company/glossary/property-based-testing/index.md","pdf":"https://engineer.company/glossary/property-based-testing/index.pdf"},"kind":"page","language":"en","title":"What is property-based testing?","url":"https://engineer.company/glossary/property-based-testing/","version":1}