alcotest

Alcotest is a lightweight and colourful test framework.
README

Alcotest is a lightweight and colourful test framework.

Alcotest exposes simple interface to perform unit tests. It exposes
a simple TESTABLE module type, a check function to assert test
predicates and a run function to perform a list of unit -> unit
test callbacks.

Alcotest provides a quiet and colorful output where only faulty runs
are fully displayed at the end of the run (with the full logs ready to
inspect), with a simple (yet expressive) query language to select the
tests to run.


Examples

A simple example:

(* Build with `ocamlbuild -pkg alcotest simple.byte` *)

(* A module with functions to test *)
module To_test = struct
  let capit letter = Char.uppercase letter
  let plus int_list = List.fold_left (fun a b -> a + b) 0 int_list
end

(* The tests *)
let capit () =
  Alcotest.(check char) "same chars"  'A' (To_test.capit 'a')

let plus () =
  Alcotest.(check int) "same ints" 7 (To_test.plus [1;1;2;3])

let test_set = [
  "Capitalize" , `Quick, capit;
  "Add entries", `Slow , plus ;
]

(* Run it *)
let () =
  Alcotest.run "My first test" [
    "test_set", test_set;
  ]

The result is a self-contained binary which displays the test results. Use
./simple.byte --help to see the runtime options.

$ ./simple.native
[OK]        test_set  0   Capitalize.
[OK]        test_set  1   Add entries.
Test Successful in 0.001s. 2 tests run.

You can filter which tests to run by supplying either the exact test name
(which would run all testcases with that name), or the exact test name
and test case number (which would run just that single test):

$ ./simple.native test test_set
Testing My first test.
[OK]              test_set          0   Capitalize.
[OK]              test_set          1   Add entries.
The full test results are available in `_build/_tests`.
Test Successful in 0.000s. 2 test run.
$ ./simple.native test test_set 1
Testing My first test.
[SKIP]              test_set          0   Capitalize.
[OK]                test_set          1   Add entries.
The full test results are available in `_build/_tests`.
Test Successful in 0.000s. 1 test run.

Note that you cannot filter by test case name (i.e. Capitalize or Add entries), you have to use
the test case number instead.

See the examples
folder for more examples.

Quick and Slow tests

In general you should use `Quick tests: tests that are ran on any
invocations of the test suite. You should only use `Slow tests for stress
tests that are ran only on occasion (typically before a release or after a major
change). These slow tests can be suppressed by passing the -q flag on the
command line, e.g.:

$ ./test.exe -q # run only the quick tests
$ ./test.exe    # run quick and slow tests

Passing custom options to the tests

In most cases, the base tests are unit -> unit functions. However,
it is also possible to pass an extra option to all the test functions
by using 'a -> unit, where 'a is the type of the extra parameter.

In order to do this, you need to specify how this extra parameter is
read on the command-line, by providing a Cmdliner term for
command-line
arguments

which explains how to parse and serialize values of type 'a (note: do not
use positional arguments, only optional arguments are supported).

For instance:

let test_nice i = Alcotest.(check int) "Is it a nice integer?" i 42

let int =
  let doc = "What is your prefered number?" in
  Cmdliner.Arg.(required & opt (some int) None & info ["n"] ~doc ~docv:"NUM")

let () =
  Alcotest.run_with_args "foo" int [
    "all", ["nice", `Quick, test_nice]
  ]

Will generate test.exe such that:

$ test.exe test
test.exe: required option -n is missing

$ test.exe test -n 42
Testing foo.
[OK]                all          0   int.

Lwt

Alcotest provides an Alcotest_lwt module that you could use to wrap
Lwt test cases. The basic idea is that instead of providing a test
function in the form unit -> unit, you provide one with the type
unit -> unit Lwt.t and alcotest-lwt calls Lwt_main.run for you.

However, there are a couple of extra features:

  • If an async exception occurs, it will cancel your test case for you
    and fail it (rather than exiting the process).

  • You get given a switch, which will be turned off when the test case
    finishes (or fails). You can use that to free up any resources.

For instance:

let free () = print_endline "freeing all resources"; Lwt.return ()

let test_lwt switch () =
  Lwt_switch.add_hook (Some switch) free;
  Lwt.async (fun () -> failwith "All is broken");
  Lwt_unix.sleep 10.

let () =
  Alcotest.run "foo" [
    "all", [
      Alcotest_lwt.test_case "one" `Quick test_lwt
    ]
  ]

Will generate:

$ test.exe
Testing foo.
[ERROR]             all          0   one.
-- all.000 [one.] Failed --
in _build/_tests/all.000.output:
freeing all resources
[failure] All is broken

Comparison with other testing frameworks

The README is pretty clear about that:

Alcotest is the only testing framework using colors!

More seriously, Alcotest is similar to ounit
but it fixes a few of the problems found in that library:

  • Alcotest has a nicer output, it is easier to see what failed and what
    succeeded and to read the log outputs of the failed tests;

  • Alcotest uses combinators to define pretty-printers and
    comparators between the things to test.

Other nice tools doing different kind of testing also exist:

  • qcheck qcheck does random
    generation and property testing (e.g. Quick Check)

  • crowbar
    and bun
    are similar to qcheck, but use compiler-directed randomness,
    e.g. it takes advantage of the AFL support the OCaml compiler.

  • ppx_inline_tests
    allows to write tests in the same file as your source-code; they
    will be run only in a special mode of compilation.

Install
Published
17 Oct 2018
Sources
alcotest-0.8.4.tbz
md5=c940f89a2bb2e23d3f1422ce61d1396b
Dependencies
result
< "1.5"
fmt
>= "0.8.0"
jbuilder
>= "1.0+beta10"
ocaml
>= "4.02.3"
Reverse Dependencies
albatross
>= "1.5.0"
alcotest-lwt
< "1.0.0"
angstrom
>= "0.7.0"
anycache
>= "0.7.4"
asak
>= "0.2"
asli
>= "0.2.0"
atd
>= "2.3.3"
atdgen
>= "2.10.0"
base64
>= "2.1.2"
bigstring
>= "0.3"
bls12-381
< "0.4.1" | >= "3.0.0"
bls12-381-js
>= "0.4.2"
capnp-rpc
< "0.6.0"
ccss
>= "1.6"
checkseum
>= "0.0.3"
cohttp
>= "0.17.0"
conduit
= "3.0.0"
conex
< "0.10.0"
cow
>= "2.2.0"
cstruct
>= "3.3.0"
datakit
>= "0.12.0"
decompress
>= "0.8"
digestif
>= "0.7"
dispatch
>= "0.4.1"
dns
>= "4.0.0"
dns-client
>= "4.6.0"
dns-forward
< "0.9.0"
dog
< "0.2.1"
dune-release
>= "1.0.0"
duration
>= "0.1.1"
eqaf
>= "0.5"
ezjsonm
>= "0.4.2" & < "1.3.0"
ezjsonm-lwt
< "1.3.0"
faraday
!= "0.2.0"
fat-filesystem
>= "0.12.0"
functoria
>= "2.2.0"
functoria-runtime
>= "2.2.0" & != "3.0.1" & < "4.0.0~beta1"
git
= "1.4.10" | = "1.5.0" | >= "1.5.2" & != "1.10.0" & < "3.0.0"
git-mirage
< "3.0.0"
git-unix
>= "1.10.0" & != "2.1.0" & < "3.0.0"
gmap
>= "0.3.0"
graphql-cohttp
>= "0.13.0"
graphql_ppx
>= "0.7.1"
hacl_x25519
>= "0.2.0"
httpaf
>= "0.2.0"
imagelib
>= "20200929"
inferno
>= "20220603"
inquire
< "0.2.0"
irmin
< "0.8.0" | >= "0.9.6" & != "0.11.1" & < "1.2.0" | >= "2.0.0" & < "2.3.0"
irmin-bench
>= "2.7.0"
irmin-chunk
< "2.0.0" | >= "2.3.0"
irmin-fs
< "2.0.0" | >= "2.3.0"
irmin-git
< "2.0.0" | >= "2.3.0"
irmin-http
< "2.0.0"
irmin-mem
< "2.0.0"
irmin-pack
>= "2.4.0" & != "2.6.1"
irmin-test
< "2.2.0"
irmin-unix
>= "1.0.0" & < "2.0.0" | >= "2.4.0" & != "2.6.1"
ke
>= "0.2"
lambdapi
>= "2.0.0"
lambdoc
>= "1.0-beta4"
lmdb
>= "1.0"
logtk
>= "1.5.1"
mdx
>= "1.6.0"
mechaml
>= "1.0.0"
merge-queues
>= "0.2.0"
merge-ropes
>= "0.2.0"
mirage
>= "4.0.0~beta1"
mirage-channel
>= "4.0.0"
mirage-flow
>= "1.0.2" & < "1.2.0"
mirage-fs-unix
>= "1.2.0"
mirage-kv
>= "2.0.0"
mirage-logs
>= "0.3.0"
mirage-runtime
>= "4.0.0~beta1"
nbd
>= "4.0.3"
ocaml-r
>= "0.5.0"
ocaml-version
>= "3.1.0"
ocamlformat
>= "0.13.0" & != "0.19.0~4.13preview"
odoc
>= "1.4.0" & < "2.1.0"
opium
>= "0.15.0"
owl
>= "0.6.0" & != "0.9.0" & != "1.0.0"
owl-base
< "0.5.0"
owl-ode
>= "0.1.0" & != "0.2.0"
pecu
>= "0.2"
pg_query
>= "0.9.6"
ppx_blob
>= "0.3.0"
ppx_graphql
>= "0.2.0"
ppx_yojson
>= "1.1.0"
prometheus
< "1.2"
protocol-9p
>= "0.3" & < "0.11.0" | >= "0.11.2"
qcheck
>= "0.18"
qcheck-core
>= "0.18"
reparse
>= "2.0.0" & < "3.0.0"
reparse-unix
< "2.1.0"
resp
< "0.10.0"
rfc1951
< "1.0.0"
rpc
>= "5.9.0"
sanddb
>= "0.2"
secp256k1
>= "0.4.1"
spin
>= "0.6.0"
tcpip
>= "2.4.2" & < "4.0.0" | >= "5.0.1"
terminal_size
>= "0.1.1"
tls
>= "0.12.0"
tyre
>= "0.4"
tyxml
>= "4.0.0"
tyxml-ppx
>= "4.3.0"
unix-errno
>= "0.3.0"
unix-fcntl
>= "0.3.0"
x509
>= "0.7.0"
xapi-rrd
>= "1.8.2"
xapi-stdext-std
>= "4.16.0"
yuscii
>= "0.2.0"
zlist
< "0.4.0"