qcheck/qtest

This module provides functions for running property-based tests.

Example

Here is a basic example to get you started. It assumes you are using gleeunit to run the tests.

import qcheck/generator
import qcheck/qtest

pub fn small_positive_or_zero_int__test() {
  use n <- qtest.given(generator.small_positive_or_zero_int())
  n + 1 == 1 + n
}

pub fn small_positive_or_zero_int__failures_shrink_to_zero__test() {
  use n <- qtest.given(generator.small_positive_or_zero_int())
  n + 1 != 1 + n
}

The first test will pass, but the second will fail with an error that may look something like this if you are using the Erlang target:

 Failures:

   1) qcheck/gen_int_test.small_positive_or_zero_int__failures_shrink_to_zero__test
      Failure: <<"TestError[original_value: 10; shrunk_value: 0; shrink_steps: 1;]">>
      stacktrace:
        qcheck_ffi.fail
      output: 

Functions

pub fn given(
  generator generator: Generator(a),
  property property: fn(a) -> Bool,
) -> Nil

A specialized version of run that uses the default configuration.

pub fn given_result(
  generator generator: Generator(a),
  property property: fn(a) -> Result(b, c),
) -> Nil

A specialized version of run_result that uses the default configuration.

pub fn run(
  config config: Config,
  generator generator: Generator(a),
  property property: fn(a) -> Bool,
) -> Nil

run(config, generator, property) runs the property function against some test cases generated by the generator function according to the specified config

The run function returns Nil if the property holds (i.e., return True for all test cases), or panics if the property does not hold for some test case a (i.e., returns False or panics).

pub fn run_result(
  config config: Config,
  generator generator: Generator(a),
  property property: fn(a) -> Result(b, c),
) -> Nil

run_result(config, generator, property) is like run but the property function returns a Result instead of a Bool.

Search Document