package alcotest-lwt
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=085c481aeedf80d766ff9ba4d9929688bed01ef390915dc28a9bb4ba7664b2ae
sha512=ca489811d3f13a2604a4b0a2b7463d611741bf8a96655e3ae1dfbeb60f2e81f589d389f12379543e5e2a31e973170134855f10d1ba94ffdc6123e34227a7d37a
Description
Lwt-based helpers for Alcotest
Published: 08 Sep 2020
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. See the manpage for details.
For information on contributing to Alcotest, see CONTRIBUTING.md.
Examples
A simple example (taken from examples/simple.ml
):
Generated by the following test suite specification:
(* Build with `ocamlbuild -pkg alcotest simple.byte` *)
(* A module with functions to test *)
module To_test = struct
let lowercase = String.lowercase_ascii
let capitalize = String.capitalize_ascii
let str_concat = String.concat ""
let list_concat = List.append
end
(* The tests *)
let test_lowercase () =
Alcotest.(check string) "same string" "hello!" (To_test.lowercase "hELLO!")
let test_capitalize () =
Alcotest.(check string) "same string" "World." (To_test.capitalize "world.")
let test_str_concat () =
Alcotest.(check string) "same string" "foobar" (To_test.str_concat ["foo"; "bar"])
let test_list_concat () =
Alcotest.(check (list int)) "same lists" [1; 2; 3] (To_test.list_concat [1] [2; 3])
(* Run it *)
let () =
let open Alcotest in
run "Utils" [
"string-case", [
test_case "Lower case" `Quick test_lowercase;
test_case "Capitalization" `Quick test_capitalize;
];
"string-concat", [ test_case "String mashing" `Quick test_str_concat ];
"list-concat", [ test_case "List mashing" `Slow test_list_concat ];
]
The result is a self-contained binary which displays the test results. Use dune exec examples/simple.exe -- --help
to see the runtime options.
Here's an example of a of failing test suite:
By default, only the first failing test log is printed to the console (and all test logs are captured on disk). Pass --show-errors
to print all error messages.
Selecting tests to execute
You can filter which tests to run by supplying a regular expression matching the names of the tests to execute, or by passing a regular expression and a comma-separated list of test numbers (or ranges of test numbers, e.g. 2,4..9
):
$ ./simple.native test '.*concat*'
Testing Utils.
[SKIP] string-case 0 Lower case.
[SKIP] string-case 1 Capitalization.
[OK] string-concat 0 String mashing.
[OK] list-concat 0 List mashing.
The full test results are available in `_build/_tests`.
Test Successful in 0.000s. 2 tests run.
$ ./simple.native test 'string-case' '1..3'
Testing Utils.
[SKIP] string-case 0 Lower case.
[OK] string-case 1 Capitalization.
[SKIP] string-concat 0 String mashing.
[SKIP] list-concat 0 List mashing.
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. Lower case
or Capitalization
), you must filter by test name & 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 () =
Lwt_main.run @@ Alcotest_lwt.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.
Dev Dependencies
None
- ambient-context-eio
- ambient-context-lwt
-
azure-cosmos-db
>= "0.1.3"
-
capnp-rpc-lwt
= "0.3"
-
capnp-rpc-mirage
>= "0.6.0"
-
capnp-rpc-unix
>= "0.6.0" & < "1.2.3"
- carton
- carton-git
- carton-lwt
-
current
>= "0.4"
-
current_git
>= "0.6.4"
- equinoxe
- equinoxe-cohttp
- equinoxe-hlc
-
git
>= "3.0.0"
- git-cohttp
- git-cohttp-mirage
- git-cohttp-unix
-
git-mirage
>= "3.0.0"
-
git-unix
>= "3.0.0"
-
gitlab-unix
< "0.1.1"
- guardian
- http-mirage-client
-
irmin
>= "2.4.0"
- irmin-containers
-
irmin-graphql
>= "2.2.0"
-
irmin-pack
!= "2.3.0" & != "2.6.1"
-
ledgerwallet-tezos
>= "0.4.0"
- letters
- mimic
- mirage-block-partition
-
mirage-vnetif-stack
< "0.6.1"
- multipart_form-lwt
-
nbd
>= "4.0.3"
- nbd-tool
-
nbd-unix
< "6.0.1"
-
obuilder
< "0.6.0"
-
ocluster
< "0.3.0"
-
opium
>= "0.19.0"
- opium-graphql
- opium-testing
- otoggl
- paf
- paf-cohttp
- pgx_lwt_unix
-
piaf
< "0.2.0"
-
prometheus-app
>= "1.2"
-
rpclib-lwt
>= "7.1.0"
-
SZXX
< "4.0.0"
- server-reason-react
-
sihl
!= "0.3.0~rc1" & < "0.4.0"
-
sihl-cache
< "0.4.0"
- sihl-core
-
sihl-email
>= "0.3.0~rc1" & < "0.4.0"
- sihl-facade
-
sihl-persistence
>= "0.3.0~rc1"
-
sihl-queue
>= "0.3.0~rc1" & < "0.4.0"
- sihl-session
-
sihl-storage
>= "0.3.0~rc1" & < "0.4.0"
-
sihl-token
< "0.4.0"
- sihl-type
-
sihl-user
>= "0.3.0~rc1" & < "0.4.0"
-
sihl-web
>= "0.3.0~rc1"
- swapfs
- terminus
- terminus-cohttp
- terminus-hlc
- tezos-008-PtEdo2Zk-test-helpers
- tezos-009-PsFLoren-test-helpers
- tezos-010-PtGRANAD-test-helpers
-
tezos-alpha-test-helpers
< "12.0"
-
tezos-baking-011-PtHangz2
>= "12.0"
-
tezos-baking-012-Psithaca
< "13.0"
-
tezos-baking-alpha
>= "12.0" & < "13.0"
-
tezos-clic
>= "8.0" & < "13.0"
-
tezos-client-004-Pt24m4xi
>= "8.0" & < "9.0"
-
tezos-client-005-PsBabyM1
>= "8.0" & < "10.2"
-
tezos-client-006-PsCARTHA
>= "8.0" & < "9.0"
-
tezos-client-007-PsDELPH1
>= "8.0" & < "13.0"
-
tezos-client-008-PtEdo2Zk
< "13.0"
- tezos-client-008-PtEdoTez
-
tezos-client-009-PsFLoren
< "13.0"
-
tezos-client-010-PtGRANAD
< "13.0"
-
tezos-client-011-PtHangz2
< "13.0"
-
tezos-client-alpha
>= "8.0" & < "13.0"
-
tezos-context
< "12.0"
-
tezos-crypto
>= "8.0" & < "9.2" | = "10.2"
-
tezos-error-monad
>= "8.0" & < "12.0"
- tezos-legacy-store
-
tezos-lwt-result-stdlib
< "12.0"
-
tezos-micheline
>= "8.0" & < "13.0"
-
tezos-mockup
>= "10.2" & < "13.0"
-
tezos-p2p
>= "8.0" & < "13.0"
-
tezos-protocol-environment
>= "8.0" & < "12.0"
-
tezos-protocol-plugin-012-Psithaca
< "13.0"
-
tezos-proxy
< "13.0"
-
tezos-requester
>= "8.0" & < "13.0"
-
tezos-rpc-http-server
>= "10.2" & < "13.0"
-
tezos-sapling
< "13.0"
-
tezos-shell
>= "8.0" & < "12.0"
-
tezos-shell-services
>= "11.0" & < "13.0"
-
tezos-signer-backends
>= "8.0" & < "13.0"
-
tezos-stdlib
>= "8.0" & < "12.0"
-
tezos-storage
>= "8.0"
-
tezos-store
< "12.0"
-
tezos-test-helpers
< "12.0"
-
tezos-test-services
< "10.2"
- universal-portal
-
vhd-format-lwt
>= "0.13.0"
-
wayland
< "2.0"
Conflicts
None