package alcotest

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

Install

Dune Dependency

Authors

Maintainers

Sources

alcotest-mirage-1.4.0.tbz
sha256=b1aaccfb2d651c902592c04953e2619169c91f797cf4f04a7dda2cab09b93ec1
sha512=8a13d5d4c8c77f115903e6b8e58160c6e6ec27870440bd38a674e9406f57f1eff299e65f006fd77728015d1a8f0ae30a714fe47e035824950a71ebfdff2cf3c9

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: 16 Apr 2021

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"
  6. astring
  7. fmt >= "0.8.7"
  8. ocaml >= "4.03.0"
  9. dune >= "2.2"

Dev Dependencies (1)

  1. cmdliner with-test & < "1.1.0"

  1. ahrocksdb
  2. albatross >= "1.5.0"
  3. alcotest-async < "1.0.0" | = "1.4.0"
  4. alcotest-lwt < "1.0.0" | = "1.4.0"
  5. alcotest-mirage = "1.4.0"
  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 != "2.3.1"
  18. arp-mirage < "2.0.0"
  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. capnp-rpc < "1.2.3"
  59. capnp-rpc-lwt < "0.3"
  60. capnp-rpc-mirage >= "0.9.0"
  61. capnp-rpc-unix >= "0.9.0" & < "1.2.3"
  62. carray
  63. carton
  64. carton-git
  65. carton-lwt >= "0.4.1"
  66. cborl
  67. ccss >= "1.6"
  68. cf-lwt
  69. chacha
  70. channel
  71. charrua-client
  72. charrua-client-lwt
  73. charrua-client-mirage < "0.11.0"
  74. charrua-server >= "1.4.1"
  75. checkseum >= "0.0.3"
  76. cid
  77. clarity-lang
  78. class_group_vdf
  79. cohttp >= "0.17.0"
  80. cohttp-curl-async
  81. cohttp-curl-lwt
  82. cohttp-eio >= "6.0.0~beta2"
  83. colombe >= "0.2.0"
  84. color
  85. conan
  86. conan-cli
  87. conan-database
  88. conan-lwt
  89. conan-unix
  90. conduit = "3.0.0"
  91. conex < "0.10.0"
  92. conex-mirage-crypto
  93. conex-nocrypto
  94. conformist
  95. cookie
  96. cow >= "2.2.0"
  97. css
  98. css-parser
  99. cstruct >= "3.3.0"
  100. cstruct-sexp
  101. ctypes-zarith
  102. cuid
  103. curly
  104. current >= "0.4"
  105. current-albatross-deployer
  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. dirsp-exchange-kbb2017
  118. dirsp-proscript-mirage
  119. dirsp-ps2ocaml
  120. dispatch >= "0.4.1"
  121. dkim
  122. dkim-bin
  123. dkim-mirage
  124. dkml-install
  125. dkml-install-installer
  126. dkml-install-runner
  127. dkml-package-console
  128. dns >= "4.0.0"
  129. dns-cli
  130. dns-client >= "4.6.0"
  131. dns-forward < "0.9.0"
  132. dns-forward-lwt-unix
  133. dns-resolver
  134. dns-server
  135. dns-tsig
  136. dnssd
  137. dnssec
  138. docfd >= "2.2.0"
  139. dog < "0.2.1"
  140. domain-name
  141. dream
  142. dream-pure
  143. duff
  144. dune-release >= "1.0.0"
  145. duration >= "0.1.1"
  146. eio < "0.12"
  147. eio_linux < "0.12"
  148. eio_windows < "0.12"
  149. emile
  150. encore
  151. eqaf >= "0.5"
  152. equinoxe
  153. equinoxe-cohttp
  154. equinoxe-hlc
  155. eris
  156. eris-lwt
  157. ezgzip
  158. ezjsonm >= "0.4.2" & < "1.3.0"
  159. ezjsonm-lwt
  160. FPauth
  161. FPauth-core
  162. FPauth-responses
  163. FPauth-strategies
  164. faraday != "0.2.0"
  165. farfadet
  166. fat-filesystem >= "0.12.0"
  167. ff
  168. ff-pbt
  169. flex-array
  170. fsevents-lwt
  171. functoria >= "2.2.0"
  172. functoria-runtime >= "2.2.0" & < "3.0.1" | = "3.1.2"
  173. geojson
  174. geoml >= "0.1.1"
  175. git = "1.4.10" | = "1.5.0" | >= "1.5.2" & != "1.10.0"
  176. git-cohttp
  177. git-cohttp-mirage
  178. git-cohttp-unix
  179. git-mirage
  180. git-unix >= "1.10.0" & != "2.1.0"
  181. gitlab-unix
  182. glicko2
  183. gmap >= "0.3.0"
  184. gobba
  185. gpt
  186. graphql
  187. graphql-async
  188. graphql-cohttp >= "0.13.0"
  189. graphql-lwt
  190. graphql_parser != "0.11.0"
  191. graphql_ppx >= "0.7.1"
  192. h1_parser
  193. h2
  194. hacl
  195. hacl-star >= "0.6.0"
  196. hacl_func
  197. hacl_x25519 >= "0.2.0"
  198. highlexer
  199. hkdf
  200. hockmd
  201. html_of_jsx
  202. http
  203. http-multipart-formdata < "2.0.0"
  204. httpaf >= "0.2.0"
  205. hvsock
  206. icalendar >= "0.1.4"
  207. imagelib >= "20200929"
  208. index
  209. inferno >= "20220603"
  210. influxdb-async
  211. influxdb-lwt
  212. inquire < "0.2.0"
  213. interval-map
  214. iomux
  215. irmin < "0.8.0" | >= "0.9.6" & != "0.11.1" & < "1.0.0" | >= "2.0.0" & != "2.3.0"
  216. irmin-bench >= "2.7.0"
  217. irmin-chunk < "1.3.0" | >= "2.3.0"
  218. irmin-cli
  219. irmin-containers
  220. irmin-fs < "1.3.0" | >= "2.3.0"
  221. irmin-git < "2.0.0" | >= "2.3.0"
  222. irmin-graphql >= "2.3.0"
  223. irmin-http < "2.0.0"
  224. irmin-mem < "1.3.0" | >= "2.3.0"
  225. irmin-pack >= "2.4.0" & != "2.6.1"
  226. irmin-pack-tools
  227. irmin-test >= "2.2.0" & < "3.0.0"
  228. irmin-tezos
  229. irmin-tezos-utils
  230. irmin-unix >= "1.0.0" & < "1.3.3" | >= "2.4.0" & != "2.6.1"
  231. irmin-watcher != "0.3.0"
  232. jekyll-format
  233. jerboa
  234. jitsu
  235. jose
  236. json-data-encoding >= "0.9"
  237. json_decoder
  238. jsonxt
  239. junit_alcotest
  240. jwto
  241. ke >= "0.2"
  242. kkmarkdown
  243. lambda-runtime
  244. lambda_streams
  245. lambda_streams_async
  246. lambdapi >= "2.0.0"
  247. lambdoc >= "1.0-beta4"
  248. ledgerwallet-tezos >= "0.2.1" & < "0.4.0"
  249. letters
  250. lmdb >= "1.0"
  251. logical
  252. logtk >= "1.6"
  253. lp
  254. lp-glpk
  255. lp-glpk-js
  256. lp-gurobi
  257. lru
  258. lt-code
  259. luv
  260. mbr-format >= "1.0.0"
  261. mdx >= "1.6.0"
  262. mec
  263. mechaml = "1.0.0" | >= "1.2.1"
  264. merge-queues >= "0.2.0"
  265. merge-ropes >= "0.2.0"
  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 < "3.1.0"
  275. mirage-crypto-ec != "0.9.2"
  276. mirage-flow >= "1.0.2" & < "1.2.0"
  277. mirage-flow-unix != "1.3.0" & < "1.5.0" | = "2.0.0" | >= "3.0.0"
  278. mirage-fs-mem
  279. mirage-fs-unix >= "1.2.0" & < "1.4.1"
  280. mirage-kv >= "2.0.0"
  281. mirage-kv-mem
  282. mirage-kv-unix >= "3.0.0"
  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 < "0.3.0"
  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. ocaml-r >= "0.4.0"
  312. ocaml-version >= "3.1.0"
  313. ocamlformat >= "0.13.0" & != "0.19.0~4.13preview" & < "0.25.1"
  314. ocamlformat-lib
  315. ocamlformat-rpc < "removed"
  316. ocamline
  317. ocluster < "0.3.0"
  318. odoc >= "1.4.0" & < "2.1.0"
  319. ohex
  320. oidc
  321. opam-0install
  322. opam-compiler
  323. opam-file-format >= "2.1.1"
  324. opentelemetry >= "0.6"
  325. opentelemetry-client-cohttp-lwt >= "0.6"
  326. opentelemetry-client-ocurl >= "0.6"
  327. opentelemetry-cohttp-lwt >= "0.6"
  328. opentelemetry-lwt >= "0.6"
  329. opium >= "0.15.0"
  330. opium-graphql
  331. opium-testing
  332. opium_kernel
  333. orewa
  334. orgeat
  335. ortac-core
  336. osnap < "0.3.0"
  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
  359. phylogenetics
  360. piaf
  361. polyglot
  362. polynomial
  363. ppx_blob >= "0.3.0"
  364. ppx_deriving_cmdliner
  365. ppx_deriving_qcheck
  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
  379. ppx_subliner
  380. ppx_units
  381. ppx_yojson >= "1.1.0"
  382. pratter
  383. prbnmcn-ucb1 >= "0.0.2"
  384. prc
  385. preface
  386. pretty_expressive
  387. prettym
  388. proc-smaps
  389. producer < "0.2.0"
  390. progress
  391. prom
  392. prometheus < "1.2"
  393. prometheus-app
  394. protocell
  395. protocol-9p >= "0.3" & < "0.11.0" | >= "0.11.2"
  396. protocol-9p-unix
  397. psq
  398. pyast
  399. qcheck >= "0.18"
  400. qcheck-alcotest
  401. qcheck-core >= "0.18"
  402. quickjs
  403. radis
  404. randii
  405. reason-standard
  406. reparse >= "2.0.0" & < "3.0.0"
  407. reparse-unix < "2.1.0"
  408. resp
  409. resp-unix >= "0.10.0"
  410. rfc1951 < "1.0.0"
  411. routes < "2.0.0"
  412. rpc >= "7.1.0"
  413. rpclib >= "7.1.0"
  414. rpclib-async
  415. rpclib-lwt >= "7.1.0"
  416. rubytt
  417. SZXX >= "4.0.0"
  418. salsa20
  419. salsa20-core
  420. sanddb >= "0.2"
  421. scaml >= "1.5.0"
  422. scrypt-kdf
  423. secp256k1 >= "0.4.1"
  424. secp256k1-internal
  425. semver >= "0.2.1"
  426. sendmail
  427. sendmail-lwt
  428. sendmsg
  429. server-reason-react
  430. session-cookie
  431. session-cookie-async
  432. session-cookie-lwt
  433. sherlodoc
  434. sihl < "0.2.0"
  435. sihl-type
  436. slug
  437. smol
  438. smol-helpers
  439. sodium-fmt
  440. solidity-alcotest
  441. spdx_licenses
  442. spectrum
  443. spin >= "0.7.0"
  444. squirrel
  445. ssh-agent
  446. ssl >= "0.6.0"
  447. stramon-lib
  448. syslog-rfc5424
  449. tcpip >= "2.4.2" & < "3.4.2" | >= "6.2.0" & < "8.0.0"
  450. tdigest < "2.1.0"
  451. terminal
  452. terminal_size >= "0.1.1"
  453. terminus
  454. terminus-cohttp
  455. terminus-hlc
  456. terml
  457. textmate-language >= "0.3.0"
  458. textrazor
  459. tezos-base-test-helpers < "13.0"
  460. tezos-bls12-381-polynomial
  461. tezos-client-base < "12.0"
  462. tezos-crypto >= "8.0" & < "9.0"
  463. tezos-lmdb
  464. tezos-plompiler = "0.1.3"
  465. tezos-plonk = "0.1.3"
  466. tezos-signer-backends >= "8.0" & < "13.0"
  467. tezos-stdlib >= "8.0" & < "12.0"
  468. tezos-test-helpers < "12.0"
  469. tftp
  470. timedesc
  471. timere
  472. timmy
  473. timmy-jsoo
  474. timmy-unix
  475. tls >= "0.12.0"
  476. toc
  477. topojson
  478. topojsone
  479. transept
  480. twostep
  481. type_eq
  482. type_id
  483. typebeat
  484. typeid >= "1.0.1"
  485. tyre >= "0.4"
  486. tyxml >= "4.0.0"
  487. tyxml-jsx
  488. tyxml-ppx >= "4.3.0"
  489. tyxml-syntax
  490. uecc
  491. ulid
  492. universal-portal
  493. unix-dirent
  494. unix-errno >= "0.3.0"
  495. unix-fcntl >= "0.3.0"
  496. unix-sys-resource
  497. unix-sys-stat
  498. unix-time
  499. unstrctrd
  500. uring < "0.4"
  501. user-agent-parser
  502. uspf
  503. uspf-lwt
  504. uspf-unix
  505. utop >= "2.13.0"
  506. validate
  507. validator
  508. vercel
  509. vpnkit
  510. wayland >= "2.0"
  511. wcwidth
  512. websocketaf
  513. x509 >= "0.7.0"
  514. xapi-rrd >= "1.8.2"
  515. xapi-stdext-date
  516. xapi-stdext-encodings
  517. xapi-stdext-std >= "4.16.0"
  518. yaml < "3.2.0"
  519. yaml-sexp
  520. yocaml
  521. yocaml_yaml
  522. yojson >= "1.6.0"
  523. yuscii >= "0.3.0"
  524. yuujinchou = "1.0.0"
  525. zar
  526. zed >= "3.2.2"
  527. zlist < "0.4.0"

Conflicts

None