Skip to main content

Enumerative property-based testing

Project description

LeanCheck for Python

This is a port of Haskell's LeanCheck to Python.

LeanCheck is an enumerative property-based testing library. It can be used to complement your unit tests.

This is a work in progress: this library is currently experimental..

The usual drill in unit testing involves making assertions about specific input-output cases of functions, such as:

assertEqual(sorted([4,2,1,3]), [1,2,3,4])

There are no arguments to the unit test.

In property-based testing (with LeanCheck) one writes more general properties that should be true for a given set of arguments.

For example: given any list, sorting it twice is the same as sorting it once. We can encode this as a function returning a boolean value:

def prop_sorted_twice(xs: list[int]) -> bool:
    return sorted(sorted(xs)) == sorted(xs)

For whatever list we provide this function, it should return True. Now one can use LeanCheck to verify this automatically:

>>> check(prop_sorted_twice)
+++ OK, passed 360 tests: prop_sorted_twice

Quick Example

$ python -i leancheck.py

>>> from leancheck import *

>>> def prop_sorted_twice(xs: list[int]) -> bool:
...     return sorted(sorted(xs)) == sorted(xs)
...

>>> check(prop_sorted_twice)
+++ OK, passed 360 tests: prop_sorted_twice

>>> def prop_sorted_len(xs: list[int]) -> bool:
...     return len(sorted(xs)) == len(xs)
...

>>> check(prop_sorted_len)
+++ OK, passed 360 tests: prop_sorted_len

>>> def prop_sorted_wrong(xs: list[int]) -> bool:
...     return sorted(xs) == xs
...

>>> check(prop_sorted_wrong)
*** Failed! Falsifiable after 6 tests:
    prop_sorted_wrong([1, 0])

Further reading

LeanCheck for Haskell is subject to a chapter in a PhD Thesis (2017).

As of 2024, Python already has a relatively popular property-based testing library called Hypothesis. While writing this port of LeanCheck, I intentionally didn't take a closer look at Hypothesis. I want to see if I would take an entirely different approach here by not getting biased of how things were implemented there. The (current) idea is mostly to stay as close as I could to the Haskell version. I will note the differences (and similarities) here, once I am done with the LeanCheck prototype and after I take a closer look at Hypothesis. LeanCheck test generation is enumerative, Hypothesis test generation is (likely) random.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

leancheck-0.0.1.2.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

leancheck-0.0.1.2-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file leancheck-0.0.1.2.tar.gz.

File metadata

  • Download URL: leancheck-0.0.1.2.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for leancheck-0.0.1.2.tar.gz
Algorithm Hash digest
SHA256 9d37f0a9da799a45fd69cbef0e183da9b614d98250b28b88ac7a71f00652f28f
MD5 c7a4e342ee14b12266cb36a12ca6b629
BLAKE2b-256 315e9cf85280f0a2a97f4f368011895c221b15755e08ed369df60b9b1f43cc30

See more details on using hashes here.

File details

Details for the file leancheck-0.0.1.2-py3-none-any.whl.

File metadata

  • Download URL: leancheck-0.0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for leancheck-0.0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d8132295ddc84a1410eeb4a00ab83e773b4671de194a32c2c44499f49ec767ae
MD5 f338a751f2e7606cc42867addbe8de53
BLAKE2b-256 5ea01e6faa7d4085d68109d1238354e3983a8af0ade9088e2d9e2ac971eae3fd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page