ExCheck Build StatusCoverage Status

Property-based testing for Elixir (QuickCheck style). It uses Erlang's triq library for underlying checking engine, and ExCheck's modules provide wrapper macros for ExUnit tests.

Installation

First add ExCheck and triq to your project's dependencies in mix.exs.

defp deps do
[
{:excheck, "~> 0.3", only: :test},
{:triq, github: "krestenkrab/triq", only: :test}
]
end

and add the following to test_helpers.exs:

ExCheck.start
# ... other helper functions
ExUnit.start

Configuration

You can also specify the amount of tests that you want to run for each property by adding the following to your config.exs:

use Mix.Config
# import "#{Mix.env}.exs" # If you want to specify different amount for each environment
# And then in this file (or different amount in each config file):
config :excheck, :number_iterations, 200

Note: This setting is not effective for ExCheck.check(module_name) at the moment (refer to #13).

Usage

The following is an testing example. ExCheck.SampleTest is the testing code for ExCheck.Sample.

Test

defmodule ExCheck.SampleTest do
use ExUnit.Case, async: false
use ExCheck
alias ExCheck.Sample
property :square do
for_all x in int, do: x * x >= 0
end
property :implies do
for_all x in int do
implies x >= 0 do
x >= 0
end
end
end
property :such_that do
for_all {x, y} in such_that({xx, yy} in {int, int} when xx < yy) do
x < y
end
end
property :concat_list do
for_all {xs, ys} in {list(int), list(int)} do
Enum.count(Sample.concat(xs, ys)) == Enum.count(xs) + Enum.count(ys)
end
end
property :push_list do
for_all {x, y} in {int, list(int)} do
result = Sample.push(x, y)
Enum.at(result, 0) == x and Enum.count(result) == Enum.count(y) + 1
end
end
# specify iteration count for running test
@tag iterations: 30
property :square_with_iteration_count do
for_all x in int, do: x * x >= 0
end
end

Code

defmodule ExCheck.Sample do
@moduledoc """
Sample logic to be tested by ExCheck (refer to sample_test.exs for tests)
"""
@doc "concatinate the list"
def concat(x, y) do
x ++ y
end
@doc "push element in the list"
def push(x, y) do
[x|y]
end
end

Run

$ MIX_ENV=test mix test test/sample_test.exs
.............................................................................................
Ran 100 tests
....x.....xx.x.xxxx.xxxx...xxxx...xx.xxxx...x.....x.x.xx...xx.x...xxx..x...xx..xxxxx..xxxx...
Ran 50 tests
.............................................................................................
Ran 100 tests
.............................................................................................
Ran 100 tests
.............................................................................................
Ran 100 tests
.
Finished in 0.2 seconds (0.1s on load, 0.03s on tests)
5 tests, 0 failures

There are some more examples at test directory.

Generators

The following generators defined in :triq are imported through "use ExCheck" statement.

Notes