MyNixOS website logo
Description

Enumerative property-based testing.

LeanCheck is a simple enumerative property-based testing library.

Properties are defined as Haskell functions returning a boolean value which should be true for all possible choices of argument values. LeanCheck applies enumerated argument values to these properties in search for a counterexample. Properties can be viewed as parameterized unit tests.

LeanCheck works by producing tiers of test values: a possibly infinite list of finite sublists of same-and-increasingly-sized values.

LeanCheck has lean core with only 200 lines of Haskell code.

LeanCheck

LeanCheck's Build Status LeanCheck on Hackage LeanCheck on Stackage LTS LeanCheck on Stackage Nightly

LeanCheck logo

LeanCheck is a simple enumerative property-based testing library. Properties are defined as Haskell functions returning a boolean value which should be True for all possible choices of argument values. LeanCheck applies enumerated argument values to these properties in search for a counterexample. Properties can be viewed as parameterized unit tests.

LeanCheck works by producing tiers of test values: a possibly infinite list of finite sublists of same-and-increasingly-sized values. This enumeration is similar to Feat's. However, the ranking and ordering of values are defined differently. The interface is also different.

Throughout this README lines that begin with the symbol > indicate a line entered into an interactive interpreter (ghci). The result of evaluating the expression is then printed on the following line.

LeanCheck implementation is easy to understand. LeanCheck's core is under 200 lines of code.

Installing

To install the latest LeanCheck version from Hackage just run:

$ cabal update
$ cabal install leancheck

Starting from Cabal v3.0, you need to pass --lib as an argument to cabal install to install packages globally on the default user environment:

$ cabal install leancheck --lib

If you already have LeanCheck installed Cabal may refuse to update to the latest version. To update, you need to reset your user's cabal installation with:

rm -rf ~/.cabal/{bin,lib,logs,share,store} ~/.ghc/*/

WARNING: the above command will erase all user-local packages.

LeanCheck has (official) packages available on Stackage, OpenSUSE, Gentoo, Arch Linux and NixOS.

Checking if properties are True

To check if properties are True, just use the function holds:: Testable a => Int -> a -> Bool. It takes two arguments: the number of values to test and a property (function returning Bool), then, it returns a boolean indicating whether the property holds. See (ghci):

> import Test.LeanCheck
> import Data.List
> holds 100 $ \xs -> sort (sort xs) == sort (xs::[Int])
True
> holds 100 $ \xs -> [] `union` xs == (xs::[Int])
False

As a rule-of-thumb, you should run holds for 500, 1 000, or 10 000 tests. With more than that you may run out-of-memory depending on the types being tested.

Finding counter examples

To find counter examples to properties, you can use the function counterExample:: Testable a => Int -> a -> Maybe [String]. It takes two arguments: the number of values to test and a property (function returning Bool). Then, it returns Nothing if no results are found or Just a list of Strings representing the offending arguments to the property. See (ghci):

> import Test.LeanCheck
> import Data.List

> counterExample 100 $ \xs -> sort (sort xs) == sort (xs::[Int])
Nothing

> counterExample 100 $ \xs -> [] `union` xs == (xs::[Int])
Just ["[0,0]"]

> counterExample 100 $ \xs ys -> xs `union` ys == ys `union` (xs::[Int])
Just ["[]","[0,0]"]

Checking properties like in SmallCheck/QuickCheck

To "check" properties like in SmallCheck and QuickCheck automatically printing results on standard output, you can use the function check:: Testable a => a -> IO ().

> import Test.LeanCheck
> import Data.List

> check $ \xs -> sort (sort xs) == sort (xs::[Int])
+++ OK, passed 200 tests.

> check $ \xs ys -> xs `union` ys == ys `union` (xs::[Int])
*** Failed! Falsifiable (after 4 tests):
[] [0,0]

The function check tests for a maximum of 200 tests. To check for a maximum of n tests, use checkForn. To get a boolean result wrapped in IO, use checkResult or checkResultFor. There is no "quiet" option, just use holds or counterExample in that case.

Testing user-defined types

LeanCheck works on properties with Listable argument types. Listable instances are declared similarly to SmallCheck:

data MyType = MyConsA
            | MyConsB Int
            | MyConsC Int Char
            | MyConsD String

instance Listable MyType where
  tiers = cons0 MyConsA
       \/ cons1 MyConsB
       \/ cons2 MyConsC
       \/ cons1 MyConsD

For types that do not have a constraning data invariant, instances can be automatically derived with Template Haskell by using deriveListable like so:

deriveListable ''MyType

The tiers function return a potentially infinite list of finite sub-lists (tiers). Each successive tier has values of increasing size.

tiers :: Listable a => [[a]]

For convenience, the function list returns a potentially infinite list of values of the bound type:

list :: Listable a => [a]

So, for example:

> take 5 (list :: [(Int,Int)])
[(0,0),(0,1),(1,0),(0,-1),(1,1)]

The list function can be used to debug your custom instances.

Listable class instances are more customizable than what is described here: check source comments or haddock documentation for details.

Standard Listable Instances

LeanCheck comes out-of-the-box with Listable instances for all types in the Haskell 2010 Language Report with the intentional exception of a few types. The leancheck-instances package aims to support types in the Haskell Platform -- $ cabal install leancheck-instances.

Providers for Tasty, test-framework and Hspec

The following providers allow including LeanCheck properties into Tasty, test-framework or Hspec test suites.

Memory usage

Due to the way it is implemented (using lists of lists), LeanCheck can be quite memory intensive if we set the maximum number of tests of a property to millions of values (YMMV).

For the default maximum number of tests (200) you should be safe on most cases. If you use 1 000 or 10 000 as the maximum number of tests for a property you're also generally safe. More than that, it is in a hit or miss basis.

For more details, see LeanCheck memory usage.

Beginner friendliness

LeanCheck strives to be beginner/student friendly both in the interface and its implementation. For instance, to understand LeanCheck's core, one does not need to understand Monads as they aren't used at all there.

In the name of keeping the implementation easy to understand, a compromise were made in terms of performance (cf. LeanCheck memory usage).

LeanCheck is mostly Haskell 98 compliant and almost Haskell 2010 compliant. With the exception of Listable derivation modules (TH and Generics), the only extension used by LeanCheck is CPP. This is to maintain compatibility with different compilers. LeanCheck even compiles and runs on Hugs98 from September 2006.

LeanCheck has 100% Haddock coverage with most functions having examples.

Further reading

For a detailed documentation of each function, see LeanCheck's Haddock documentation.

For an introduction to property-based testing and a step-by-step guide to LeanCheck, see the tutorial on property-based testing with LeanCheck (doc/tutorial.md in the source repository).

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

LeanCheck has a list of frequently asked questions and answers.

Metadata

Version

1.0.2

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows