Module B0_testing.Test

Testing structure and combinators.

Testing structure

val test : string -> (unit -> unit) -> unit

test name f runs the test in f. The test fails if it raises an unexpected exception. name is logged before f is executed. This is typically called this way:

let mytest () =
  Test.test "Something" @@ fun () ->
  assert (1 = 1); …
val main : (unit -> unit) -> int

main f executes f () and reports the testing status. The returned integer is the number of failing tests which should be given to exit.

f typically calls function that call test. Your typical main should look like this::

let main () =
  Test.main () @@ fun () ->
  my_test ()

let () = if !Sys.interactive then exit (main ())

This structure ensures you can load and run its components in the toplevel, e.g. via b0 -- .ocaml.ocaml

Test combinators

val repeat : fail:(int -> 'b, Stdlib.Format.formatter, unit, unit) Stdlib.format4 -> int -> (int -> 'a) -> unit

repeat ~fail n f calls f with n to 1 stopping if it fails and logging log fail n.

val invalid_arg : (unit -> 'a) -> unit

invalid_arg f tests that f () raises Invalid_argument.

val failure : (unit -> 'a) -> unit

failure f tests that f () raises Failure.

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

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

Testing logging

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

log fmt … logs a message formatted by fmt

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

log_fail fmt … is like log but for failures.