quick_chex v0.4.0 QuickChex

main module with the main macros that you can use when writing property-based tests.

add use QuickChex at the top of your ExUnit test module

Example

defmodule MyModuleTest do
  use ExUnit.Case, async: true
  use QuickChex # <- add this!
end

Summary

Macros

check a property by giving the property name and a list of settings

specify a property with a single generate parameter

same as property/2 with two generated parameters

same as property/2 with three generated parameters

Macros

check(name, check_name \\ nil, settings)

check a property by giving the property name and a list of settings

settings:

  • with a list of generators (or values) to be passed to the property. Be careful! the number of generators must match the number of arguments of the property
  • iterations integer representing the number of iterations
  • only_if a function with the same arity as the number of generators in the with settings. If the function resturns true than the generated values are considered acceptable for the property, otherwise another attempt is made until a suitable array of values is found. Use this with caution, as it could slow down your test suite considerably.

a full example

check :my_property,
  with: [non_neg_integer, non_neg_integer],
  itarations: 100,
  only_if: fn num1, num2 -> num1 > num2 end

this checks a property with two non negative integer args. The only_if function ensures that the first number is greater than the second one.

usually there is a better approach

check :my_property,
  with: [{:non_neg_integer, [0, 1000], {:non_neg_integer, [1001, 2000]}],
  itarations: 100

by better specifying the generators we can omit the only_if block and speed up our test suite. Use the only_if block only if generator constraints are not enough.

property(name, param1, list)

specify a property with a single generate parameter

A property has a name to be identified by checks, a param1 that will be generated, and a body to specify the requirements

Example

for a negate property of a Test.negate/1 function you could write a property like this:

property :negate, value do
  assert not value === Test.negate(value)
end

property :negate_two_time_means_doing_nothing, value do
  res = value
  |> Test.negate
  |> Test.negate
  assert value === res
end
property(name, param1, param2, list)

same as property/2 with two generated parameters

Example

a property for the Test.add/2 function

property :add_is_commutative, n1, n2 do
  assert Test.add(n1, n2) === Test.add(n2, n1)
end
property(name, param1, param2, param3, list)

same as property/2 with three generated parameters