Module B0_testing.Test

Testing structure and combinators.

See an example.

Warning. Currently the testing combinators are not thread-safe. They will be made thread-safe when OCaml 5.0 is required.

Source positions

type pos = string * int * int * int

The type for test source positions. This is the type of Stdlib.__POS__.

It is an optional argument of most combinators to short-circuit the best effort failure location tracking performed via backtraces which may be unreliable in certain scenarios (e.g. closures). The idea is that the argument is simply used with Stdlib.__POS__. For example:

Test.string ~__POS__ s0 s1;

Note that at the moment not all functions make use of the argument.

Formatters

module Fmt : sig ... end

Formatters for test runners and tested values.

Test structure

val main : ?__POS__:pos -> (unit -> unit) -> int

main f executes f (), logs the resulting testing status and returns 0 if all tests passed and 1 otherwise for use with Stdlib.exit. Usually f calls functions that call test. See the example.

val test : ?__POS__:pos -> string -> (unit -> unit) -> unit

test name f logs "Test %s" name and executes f (). The test fails if any failure is reported in f or if f raises an unexpected exception. See the example.

val stop : ?__POS__:pos -> unit -> 'a

stop stops the current loop, test, or main. Note that this does not increment failed checks, use fail or failstop for that.

Passing and failing checks

A test is usually made of many checks. If a check fails the test fails.

val pass : ?__POS__:pos -> unit -> unit

pass () increments the number of successfull checks.

val fail : ?__POS__:pos -> ('a, Stdlib.Format.formatter, unit, unit) Stdlib.format4 -> 'a

failf fmt… increments the number of failed checks. The test continues to execute, use stop and failstop to stop the test.

val failstop : ?__POS__:pos -> ('a, Stdlib.Format.formatter, unit, 'b) Stdlib.format4 -> 'a

failf fmt… is like fail but stops the test.

Logging

val log : ?__POS__:pos -> ('a, Stdlib.Format.formatter, unit, unit) Stdlib.format4 -> 'a

log fmt … logs a message formatted by fmt.

val log_fail : ?__POS__:pos -> ('a, Stdlib.Format.formatter, unit, unit) Stdlib.format4 -> 'a

log_fail fmt … is like log but formatted for failures. This does not increment failed checks, use fail to register a failure.

Test combinators

Loops and blocks

val range : ?kind:string -> first:int -> last:int -> ?__POS__:pos -> (int -> unit) -> unit

range ~first ~last f calls f n with n ranging over the range [first;last]. If a failure occurs for n, logs "%s in range [%d;%d] failed on %d" kind first last n. If f performs a failstop the loop is stopped but not the test. kind defaults to "Test".

val block : ?fail:(?__POS__:pos -> int -> checks:int -> unit) -> ?__POS__:pos -> (unit -> unit) -> unit

block ~fail f runs f (), if that results in n > 0 failed checks, fail n ~checks is called afterwards with checks the total number of checks that were performed. If f peforms a failstop the block is stopped but not the test. It is possible to fail stop the test by stoppping in fail.

Assertions

val holds : ?msg:string -> ?__POS__:pos -> bool -> unit

holds b asserts that b is true. msg is logged if the assertion fails.

Exceptions

val raises : exn -> ?__POS__:pos -> (unit -> 'a) -> unit

raises exn f tests that f () raises exception exn.

val raises' : (exn -> bool) -> ?__POS__:pos -> (unit -> 'a) -> unit

raises is_exn f tests that f () raises an exception exn that satisfies is_exn exn.

val invalid_arg : ?__POS__:pos -> (unit -> 'a) -> unit

invalid_arg f tests that f () raises Invalid_argument _.

val failure : ?__POS__:pos -> (unit -> 'a) -> unit

failure f tests that f () raises Failure _.

Values

module Eq : sig ... end

Equality testers.

val eq : 'a Eq.t -> ?__POS__:pos -> 'a -> 'a -> unit

eq t v0 v1 assert v0 and v1 are equal are equal using equality tester t.

val neq : 'a Eq.t -> ?__POS__:pos -> 'a -> 'a -> unit

neq t v0 v1 assert v0 and v1 are not equal using equality tester t.

Stdlib values

val bool : ?__POS__:pos -> bool -> bool -> unit

bool b0 b1 asserts that b0 and b1 are equal.

val char : ?__POS__:pos -> char -> char -> unit

char c0 c1 asserta that c0 and c1 are equal.

val int : ?__POS__:pos -> int -> int -> unit

int i0 i1 asserts that i0 and i1 are equal.

val int32 : ?__POS__:pos -> int32 -> int32 -> unit

int32 i0 i1 asserts that i0 and i1 are equal.

val uint32 : ?__POS__:pos -> int32 -> int32 -> unit

uint32 i0 i1 asserts that i0 and i1 are equal.

val int64 : ?__POS__:pos -> int64 -> int64 -> unit

int64 i0 i1 asserts that i0 and i1 are equal.

val uint64 : ?__POS__:pos -> int64 -> int64 -> unit

uint64 i0 i1 asserts that i0 and i1 are equal.

val nativeint : ?__POS__:pos -> nativeint -> nativeint -> unit

nativeint i0 i1 asserts that i0 and i1 are equal.

val float : ?__POS__:pos -> float -> float -> unit

float f0 f1 asserts that f0 and f1 are equal. This uses Float.equal so it can be used to assert NaN values, also you should be aware of the limitations that are involved in testing floats for equality.

val string : ?__POS__:pos -> string -> string -> unit

string s0 s1 asserts that s0 and s1 are equal.

val binary_string : ?__POS__:pos -> string -> string -> unit

binary_string s0 s1 asserts that s0 and s1 are equal assuming that s0 and s1 is binary data.

val styled_string : ?__POS__:pos -> string -> string -> unit

styled_string s0 s1 asserts that s0 and s1 are equal assuming that s0 and s1 have ANSI escape sequences.

val bytes : ?__POS__:pos -> bytes -> bytes -> unit

string s0 s1 asserts that s0 and s1 are equal.

val list : ?elt:'a Eq.t -> ?__POS__:pos -> 'a list -> 'a list -> unit

list ~elt l0 l1 assert lists l0 and l1 are equal using elt to test elements (default to Eq.any).

val array : ?elt:'a Eq.t -> ?__POS__:pos -> 'a array -> 'a array -> unit

array ~elt a0 a1 assert arrays a0 and a1 are equal using elt to test elements (defaults to Eq.any).

Options

val get_some : ?__POS__:pos -> 'a option -> 'a

get_some gets the value of Some _ and fails and stops if that is not possible.

val option : ?some:'a Eq.t -> ?__POS__:pos -> 'a option -> 'a option -> unit

option ~some:(module M) v0 v1 asserts options v0 and v1 are equal according to M's equality (otherwise Stdlib.(=) is used).

Results

val get_ok : ?__POS__:pos -> ('a, string) Stdlib.result -> 'a

get_ok gets the value of Ok _ and failstops if that is not possible. Calls pass on success.

val get_ok' : ?error:'e Eq.t -> ?__POS__:pos -> ('a, 'e) Stdlib.result -> 'a

get_ok' is like get_ok but uses error to render the error.

val result : ?ok:'a Eq.t -> ?__POS__:pos -> ('a, string) Stdlib.result -> ('a, string) Stdlib.result -> unit

result ~ok r0 r1 asserts results r0 and r1 are equal according to ok (defaults to Eq.any).

val result' : ?ok:'a Eq.t -> ?error:'e Eq.t -> ?__POS__:pos -> ('a, 'e) Stdlib.result -> ('a, 'e) Stdlib.result -> unit

result ~ok ~error r0 r1 asserts results r0 and r1 are equal according to ok and error (both default to El.eq

Randomized testing

module Rand : sig ... end

Randomized testing.

module Cli : sig ... end

Command line