Name: alcotest
Owner: MirageOS
Description: A lightweight and colourful test framework
Created: 2013-12-12 16:58:41.0
Updated: 2018-03-30 18:50:51.0
Pushed: 2018-03-25 11:36:52.0
Homepage: null
Size: 341
Language: OCaml
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
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.
A simple example:
uild with `ocamlbuild -pkg alcotest simple.byte` *)
module with functions to test *)
le To_test = struct
t capit letter = Char.uppercase letter
t plus int_list = List.fold_left (fun a b -> a + b) 0 int_list
he tests *)
capit () =
cotest.(check char) "same chars" 'A' (To_test.capit 'a')
plus () =
cotest.(check int) "same ints" 7 (To_test.plus [1;1;2;3])
test_set = [
apitalize" , `Quick, capit;
dd entries", `Slow , plus ;
un it *)
() =
cotest.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
test_set 0 Capitalize.
test_set 1 Add entries.
Successful in 0.001s. 2 tests run.
See the examples folder for more examples.
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
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:
test_nice i = Alcotest.(check int) "Is it a nice integer?" i 42
int =
t doc = "What is your prefered number?" in
dliner.Arg.(required & opt (some int) None & info ["n"] ~doc ~docv:"NUM")
() =
cotest.run_with_args "foo" int [
"all", ["nice", `Quick, test_nice]
Will generate test.exe
such that:
st.exe test
.exe: required option -n is missing
st.exe test -n 42
ing foo.
all 0 int.
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:
free () = print_endline "freeing all resources"; Lwt.return ()
test_lwt switch () =
t_switch.add_hook (Some switch) free;
t.async (fun () -> failwith "All is broken");
t_unix.sleep 10.
() =
cotest.run "foo" [
"all", [
Alcotest_lwt.test_case "one" `Quick test_lwt
]
Will generate:
st.exe
ing foo.
OR] all 0 one.
ll.000 [one.] Failed --
build/_tests/all.000.output:
ing all resources
lure] All is broken