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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file leancheck-0.0.2.tar.gz.
File metadata
- Download URL: leancheck-0.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4a1dbd2f78713873dc3901819555c8349aa80312255921c0504fddb8ce350a0
|
|
| MD5 |
8a5d1731497f8e107da60ce58f8a351f
|
|
| BLAKE2b-256 |
61d19b7f00749fd416150582e3109b9feb5eaad34551517c0599771d04d7ff99
|
File details
Details for the file leancheck-0.0.2-py3-none-any.whl.
File metadata
- Download URL: leancheck-0.0.2-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a1bc104c877a83a63ff4264d3b0961627dfe3e10c8462583c70f8edcc095235
|
|
| MD5 |
26b84d3f3b83f87196a7de4ba6ffd1dd
|
|
| BLAKE2b-256 |
acef100615bb3856695e5a2488b3278bb82d18afa0e8d6fe2c36aceef1af8f7c
|