package alcotest

  1. Overview
  2. Docs
Alcotest is a lightweight and colourful test framework

Install

Dune Dependency

Authors

Maintainers

Sources

alcotest-mirage-1.2.3.tbz
sha256=085c481aeedf80d766ff9ba4d9929688bed01ef390915dc28a9bb4ba7664b2ae
sha512=ca489811d3f13a2604a4b0a2b7463d611741bf8a96655e3ae1dfbeb60f2e81f589d389f12379543e5e2a31e973170134855f10d1ba94ffdc6123e34227a7d37a

Description

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.

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.

Dependencies (9)

  1. uutf >= "1.0.0"
  2. stdlib-shims
  3. re >= "1.7.2"
  4. uuidm
  5. cmdliner >= "1.0.3" & < "1.1.0"
  6. astring
  7. fmt >= "0.8.7"
  8. ocaml >= "4.03.0"
  9. dune >= "2.2"

Dev Dependencies (1)

  1. odoc with-doc

  1. ahrocksdb
  2. albatross >= "1.5.0"
  3. alcotest-async < "1.0.0" | = "1.2.3"
  4. alcotest-lwt < "1.0.0" | = "1.2.3"
  5. alcotest-mirage = "1.2.3"
  6. alg_structs_qcheck
  7. ambient-context
  8. ambient-context-eio
  9. ambient-context-lwt
  10. angstrom >= "0.7.0"
  11. ansi >= "0.6.0"
  12. anycache >= "0.7.4"
  13. anycache-async
  14. anycache-lwt
  15. archetype >= "1.4.2"
  16. archi
  17. arp
  18. arp-mirage
  19. arrakis
  20. art
  21. asak >= "0.2"
  22. asli >= "0.2.0"
  23. asn1-combinators >= "0.2.2"
  24. atd >= "2.3.3"
  25. atdgen >= "2.10.0"
  26. atdpy
  27. atdts
  28. base32
  29. base64 >= "2.1.2" & < "3.2.0" | >= "3.4.0"
  30. bastet
  31. bastet_async
  32. bastet_lwt
  33. bech32
  34. bechamel >= "0.5.0"
  35. bigarray-overlap
  36. bigstringaf
  37. bitlib
  38. blake2
  39. bloomf
  40. bls12-381 < "0.4.1" | >= "3.0.0" & < "18.0"
  41. bls12-381-hash
  42. bls12-381-js >= "0.4.2"
  43. bls12-381-js-gen >= "0.4.2"
  44. bls12-381-legacy
  45. bls12-381-signature
  46. bls12-381-unix
  47. blurhash
  48. builder-web
  49. bulletml
  50. bytebuffer
  51. ca-certs
  52. ca-certs-nss
  53. cactus
  54. caldav
  55. calendar >= "3.0.0"
  56. callipyge
  57. camlix
  58. camlkit
  59. camlkit-base
  60. capnp-rpc < "1.2.3"
  61. capnp-rpc-lwt < "0.3"
  62. capnp-rpc-mirage >= "0.9.0"
  63. capnp-rpc-unix >= "0.9.0" & < "1.2.3"
  64. carray
  65. carton
  66. carton-git
  67. carton-lwt
  68. cborl
  69. ccss >= "1.6"
  70. cf-lwt
  71. chacha
  72. channel
  73. charrua-client
  74. charrua-client-lwt
  75. charrua-client-mirage < "0.11.0"
  76. checkseum >= "0.0.3"
  77. cid
  78. clarity-lang
  79. class_group_vdf
  80. cohttp >= "0.17.0"
  81. cohttp-curl-async
  82. cohttp-curl-lwt
  83. cohttp-eio >= "6.0.0~beta2"
  84. colombe >= "0.2.0"
  85. color
  86. conan
  87. conan-cli
  88. conan-database
  89. conan-lwt
  90. conan-unix
  91. conduit = "3.0.0"
  92. conex < "0.10.0"
  93. conex-mirage-crypto
  94. conex-nocrypto
  95. conformist
  96. cookie
  97. cow >= "2.2.0"
  98. css
  99. css-parser
  100. cstruct >= "3.3.0"
  101. cstruct-sexp
  102. ctypes-zarith
  103. cuid
  104. curly
  105. current >= "0.4"
  106. current_git >= "0.6.4"
  107. current_incr
  108. cwe_checker
  109. data-encoding
  110. datakit >= "0.12.0"
  111. datakit-bridge-github >= "0.12.0"
  112. datakit-ci
  113. datakit-client-git >= "0.12.0"
  114. decompress >= "0.8" & < "1.5.3"
  115. depyt
  116. digestif >= "0.8.1"
  117. dispatch >= "0.4.1"
  118. dkim
  119. dkim-bin
  120. dkim-mirage
  121. dns >= "4.0.0"
  122. dns-cli
  123. dns-client >= "4.6.0"
  124. dns-forward < "0.9.0"
  125. dns-forward-lwt-unix
  126. dns-resolver
  127. dns-server
  128. dns-tsig
  129. dnssd
  130. dnssec
  131. docfd >= "2.2.0"
  132. dog < "0.2.1"
  133. domain-name
  134. dot-merlin-reader >= "5.3~5.3preview"
  135. dream
  136. dream-pure
  137. duff
  138. dune-release >= "1.0.0"
  139. duration >= "0.1.1"
  140. emile
  141. encore
  142. eqaf >= "0.5"
  143. equinoxe
  144. equinoxe-cohttp
  145. equinoxe-hlc
  146. eris
  147. eris-lwt
  148. ezgzip
  149. ezjsonm >= "0.4.2" & < "1.3.0"
  150. ezjsonm-lwt
  151. FPauth
  152. FPauth-core
  153. FPauth-responses
  154. FPauth-strategies
  155. faraday != "0.2.0"
  156. farfadet
  157. fat-filesystem >= "0.12.0"
  158. ff
  159. ff-pbt
  160. fiat-p256
  161. flex-array
  162. fsevents-lwt
  163. functoria >= "2.2.0"
  164. functoria-runtime >= "2.2.0" & != "3.0.1" & < "4.0.0~beta1"
  165. geojson
  166. geoml >= "0.1.1"
  167. git = "1.4.10" | = "1.5.0" | >= "1.5.2" & != "1.10.0"
  168. git-cohttp
  169. git-cohttp-mirage
  170. git-cohttp-unix
  171. git-mirage
  172. git-split
  173. git-unix >= "1.10.0" & != "2.1.0"
  174. git_split
  175. gitlab-unix
  176. glicko2
  177. gmap >= "0.3.0"
  178. gobba
  179. gpt
  180. graphql
  181. graphql-async
  182. graphql-cohttp >= "0.13.0"
  183. graphql-lwt
  184. graphql_parser != "0.11.0"
  185. graphql_ppx >= "0.7.1"
  186. h1
  187. h1_parser
  188. h2
  189. hacl
  190. hacl-star >= "0.6.0" & < "0.7.2"
  191. hacl_func
  192. hacl_x25519 >= "0.2.0"
  193. highlexer
  194. hkdf
  195. hockmd
  196. html_of_jsx
  197. http
  198. http-multipart-formdata < "2.0.0"
  199. httpaf >= "0.2.0"
  200. httpun
  201. httpun-ws
  202. hvsock
  203. icalendar >= "0.1.4"
  204. imagelib >= "20200929"
  205. index
  206. inferno >= "20220603"
  207. influxdb-async
  208. influxdb-lwt
  209. inquire < "0.2.0"
  210. interval-map
  211. iomux
  212. irmin < "0.8.0" | >= "0.9.6" & != "0.11.1" & < "1.0.0" | >= "2.0.0" & != "2.3.0"
  213. irmin-bench >= "2.7.0"
  214. irmin-chunk < "1.3.0" | >= "2.3.0"
  215. irmin-cli
  216. irmin-containers
  217. irmin-fs < "1.3.0" | >= "2.3.0"
  218. irmin-git < "2.0.0" | >= "2.3.0"
  219. irmin-graphql >= "2.3.0"
  220. irmin-http < "2.0.0"
  221. irmin-mem < "1.3.0" | >= "2.3.0"
  222. irmin-pack >= "2.4.0" & != "2.6.1"
  223. irmin-pack-tools
  224. irmin-test >= "2.2.0" & < "3.0.0"
  225. irmin-tezos
  226. irmin-tezos-utils
  227. irmin-unix >= "1.0.0" & < "1.3.3" | >= "2.4.0" & != "2.6.1"
  228. irmin-watcher
  229. jekyll-format
  230. jerboa
  231. jitsu
  232. jose
  233. json-data-encoding >= "0.9"
  234. json_decoder
  235. jsonxt
  236. junit_alcotest
  237. jwto
  238. kdf
  239. ke >= "0.2"
  240. kkmarkdown
  241. lambda-runtime
  242. lambda_streams
  243. lambda_streams_async
  244. lambdapi >= "2.0.0"
  245. lambdoc >= "1.0-beta4"
  246. ledgerwallet-tezos >= "0.2.1" & < "0.4.0"
  247. letters
  248. lmdb >= "1.0"
  249. logical
  250. logtk >= "1.6"
  251. lp
  252. lp-glpk
  253. lp-glpk-js
  254. lp-gurobi
  255. lru
  256. lt-code
  257. luv
  258. mbr-format >= "1.0.0"
  259. mdx >= "1.6.0"
  260. mec
  261. mechaml >= "1.0.0"
  262. merge-queues >= "0.2.0"
  263. merge-ropes >= "0.2.0"
  264. merlin >= "4.17.1-414" & < "5.0-502" | >= "5.2.1-502"
  265. merlin-lib >= "4.17.1-414" & < "5.0-502" | >= "5.2.1-502"
  266. metrics
  267. middleware
  268. mimic
  269. minicaml = "0.3.1" | >= "0.4"
  270. mirage >= "4.0.0~beta1"
  271. mirage-block-partition
  272. mirage-block-ramdisk >= "0.3"
  273. mirage-channel >= "4.0.0"
  274. mirage-channel-lwt
  275. mirage-crypto-ec
  276. mirage-flow >= "1.0.2" & < "1.2.0"
  277. mirage-flow-unix
  278. mirage-fs-mem
  279. mirage-fs-unix >= "1.2.0"
  280. mirage-kv >= "2.0.0"
  281. mirage-kv-mem
  282. mirage-kv-unix
  283. mirage-logs >= "0.3.0"
  284. mirage-nat
  285. mirage-net-unix >= "2.3.0"
  286. mirage-runtime >= "4.0.0~beta1" & < "4.5.0"
  287. mirage-tc
  288. mjson
  289. mmdb
  290. mnd
  291. monocypher
  292. mrmime >= "0.2.0"
  293. mrt-format
  294. msgpck >= "1.6"
  295. mssql >= "2.0.3"
  296. multibase
  297. multihash
  298. multihash-digestif
  299. multipart-form-data
  300. multipart_form
  301. multipart_form-eio
  302. multipart_form-lwt
  303. named-pipe
  304. nanoid
  305. nbd >= "4.0.3"
  306. nbd-tool
  307. nloge
  308. nocoiner
  309. non_empty_list
  310. OCADml >= "0.6.0"
  311. obatcher
  312. ocaml-index >= "1.1"
  313. ocaml-r >= "0.4.0"
  314. ocaml-version >= "3.1.0"
  315. ocamlformat >= "0.13.0" & != "0.19.0~4.13preview" & < "0.25.1"
  316. ocamlformat-rpc < "removed"
  317. ocamline
  318. ocluster < "0.3.0"
  319. odoc >= "1.4.0" & < "2.1.0"
  320. ohex
  321. oidc
  322. opam-0install
  323. opam-0install-cudf >= "0.5.0"
  324. opam-compiler
  325. opam-file-format >= "2.1.1"
  326. opentelemetry >= "0.6"
  327. opentelemetry-client-cohttp-lwt >= "0.6"
  328. opentelemetry-client-ocurl >= "0.6"
  329. opentelemetry-cohttp-lwt >= "0.6"
  330. opentelemetry-lwt >= "0.6"
  331. opium >= "0.15.0"
  332. opium-graphql
  333. opium-testing
  334. opium_kernel
  335. orewa
  336. ortac-core
  337. osx-acl
  338. osx-attr
  339. osx-cf
  340. osx-fsevents
  341. osx-membership
  342. osx-mount
  343. osx-xattr
  344. otoggl
  345. owl >= "0.6.0" & != "0.9.0" & != "1.0.0"
  346. owl-base < "0.5.0"
  347. owl-ode >= "0.1.0" & != "0.2.0"
  348. owl-symbolic
  349. passmaker
  350. patch
  351. pbkdf
  352. pecu >= "0.2"
  353. pf-qubes
  354. pg_query >= "0.9.6"
  355. pgx >= "1.0"
  356. pgx_unix >= "1.0"
  357. pgx_value_core
  358. pgx_value_ptime < "2.2"
  359. phylogenetics
  360. piaf
  361. polyglot
  362. polynomial
  363. ppx_blob >= "0.3.0"
  364. ppx_deriving_cmdliner
  365. ppx_deriving_ezjsonm
  366. ppx_deriving_rpc
  367. ppx_deriving_yaml
  368. ppx_graphql >= "0.2.0"
  369. ppx_inline_alcotest
  370. ppx_parser
  371. ppx_protocol_conv >= "5.0.0"
  372. ppx_protocol_conv_json >= "5.0.0"
  373. ppx_protocol_conv_jsonm >= "5.0.0"
  374. ppx_protocol_conv_msgpack >= "5.0.0"
  375. ppx_protocol_conv_xml_light >= "5.0.0"
  376. ppx_protocol_conv_xmlm
  377. ppx_protocol_conv_yaml >= "5.0.0"
  378. ppx_repr < "0.4.0"
  379. ppx_subliner
  380. ppx_units
  381. ppx_yojson >= "1.1.0"
  382. pratter < "4.0.0"
  383. prc
  384. preface
  385. pretty_expressive
  386. prettym
  387. proc-smaps
  388. producer < "0.2.0"
  389. progress < "0.2.0"
  390. prom
  391. prometheus < "1.2"
  392. prometheus-app
  393. protocell
  394. protocol-9p >= "0.3" & < "0.11.0" | >= "0.11.2"
  395. protocol-9p-unix
  396. psq
  397. qcheck >= "0.18"
  398. qcheck-alcotest
  399. qcheck-core >= "0.18"
  400. quickjs
  401. radis
  402. randii
  403. reason-standard
  404. red-black-tree
  405. reparse >= "2.0.0" & < "3.0.0"
  406. reparse-unix < "2.1.0"
  407. resp
  408. resp-unix >= "0.10.0"
  409. rfc1951 < "1.0.0"
  410. routes < "2.0.0"
  411. rpc >= "7.1.0"
  412. rpclib >= "7.1.0"
  413. rpclib-async
  414. rpclib-lwt >= "7.1.0"
  415. rpmfile < "0.3.0"
  416. rpmfile-eio
  417. rpmfile-unix
  418. rubytt
  419. SZXX >= "4.0.0"
  420. salsa20
  421. salsa20-core
  422. sanddb >= "0.2"
  423. scaml >= "1.5.0"
  424. scrypt-kdf
  425. secp256k1 >= "0.4.1"
  426. secp256k1-internal
  427. semver >= "0.2.1"
  428. sendmail
  429. sendmail-lwt
  430. sendmail-miou-unix
  431. sendmail-mirage
  432. sendmsg
  433. server-reason-react
  434. session-cookie
  435. session-cookie-async
  436. session-cookie-lwt
  437. sherlodoc
  438. sihl < "0.2.0"
  439. sihl-type
  440. slug
  441. smaws-clients
  442. smaws-lib
  443. sodium-fmt
  444. solidity-alcotest
  445. spin >= "0.7.0"
  446. squirrel
  447. ssh-agent
  448. ssl >= "0.6.0"
  449. stramon-lib
  450. styled-ppx
  451. swapfs
  452. syslog-rfc5424
  453. tcpip >= "2.4.2" & < "4.0.0" | >= "5.0.1" & < "7.0.0"
  454. tdigest < "2.1.0"
  455. term-indexing
  456. term-tools
  457. terminal_size >= "0.1.1"
  458. terminus
  459. terminus-cohttp
  460. terminus-hlc
  461. terml
  462. testo
  463. testo-lwt
  464. textrazor
  465. tezos-base-test-helpers < "13.0"
  466. tezos-bls12-381-polynomial
  467. tezos-client-base < "12.0"
  468. tezos-crypto >= "8.0" & < "9.0"
  469. tezos-lmdb
  470. tezos-plompiler = "0.1.3"
  471. tezos-plonk = "0.1.3"
  472. tezos-signer-backends >= "8.0" & < "13.0"
  473. tezos-stdlib >= "8.0" & < "12.0"
  474. tezos-test-helpers < "12.0"
  475. tftp
  476. timedesc
  477. timere
  478. tls >= "0.12.0"
  479. toc
  480. topojson
  481. topojsone
  482. transept
  483. twostep
  484. type_eq
  485. type_id
  486. typebeat
  487. typeid >= "1.0.1"
  488. tyre >= "0.4"
  489. tyxml >= "4.0.0"
  490. tyxml-jsx
  491. tyxml-ppx >= "4.3.0"
  492. tyxml-syntax
  493. uecc
  494. ulid
  495. universal-portal
  496. unix-dirent
  497. unix-errno >= "0.3.0"
  498. unix-fcntl >= "0.3.0"
  499. unix-sys-resource
  500. unix-sys-stat
  501. unix-time
  502. unstrctrd
  503. user-agent-parser
  504. uspf
  505. uspf-lwt
  506. uspf-mirage
  507. uspf-unix
  508. utop >= "2.13.0"
  509. validate
  510. validator
  511. vercel
  512. vhd-format-lwt >= "0.13.0"
  513. vpnkit
  514. wayland >= "2.0"
  515. wcwidth
  516. websocketaf
  517. x509 >= "0.7.0"
  518. xapi-rrd >= "1.8.2"
  519. xapi-stdext-date
  520. xapi-stdext-encodings
  521. xapi-stdext-std >= "4.16.0"
  522. yaml < "3.2.0"
  523. yaml-sexp
  524. yocaml < "2.0.0"
  525. yocaml_syndication = "2.0.0"
  526. yocaml_yaml < "2.0.0"
  527. yojson >= "1.6.0"
  528. yojson-five
  529. yuscii >= "0.3.0"
  530. yuujinchou = "1.0.0"
  531. zar
  532. zed >= "3.2.2"
  533. zlist < "0.4.0"

Conflicts

None

OCaml

Innovation. Community. Security.