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-logs >= "0.3.0"
  283. mirage-nat
  284. mirage-net-unix >= "2.3.0"
  285. mirage-runtime >= "4.0.0~beta1" & < "4.5.0"
  286. mirage-tc
  287. mjson
  288. mmdb < "0.3.0"
  289. mnd
  290. monocypher
  291. mrmime >= "0.2.0"
  292. mrt-format
  293. msgpck >= "1.6"
  294. mssql >= "2.0.3"
  295. multibase
  296. multihash
  297. multihash-digestif
  298. multipart-form-data
  299. multipart_form
  300. multipart_form-lwt
  301. named-pipe
  302. nanoid
  303. nbd >= "4.0.3"
  304. nbd-tool
  305. nloge
  306. nocoiner
  307. non_empty_list
  308. OCADml >= "0.6.0"
  309. ocaml-r >= "0.4.0"
  310. ocaml-version >= "3.1.0"
  311. ocamlformat >= "0.13.0" & != "0.19.0~4.13preview" & < "0.25.1"
  312. ocamlformat-lib
  313. ocamlformat-rpc < "removed"
  314. ocamline
  315. ocluster < "0.3.0"
  316. odoc >= "1.4.0" & < "2.1.0"
  317. ohex
  318. oidc
  319. opam-0install
  320. opam-compiler
  321. opam-file-format >= "2.1.1"
  322. opentelemetry >= "0.6"
  323. opentelemetry-client-cohttp-lwt >= "0.6"
  324. opentelemetry-client-ocurl >= "0.6"
  325. opentelemetry-cohttp-lwt >= "0.6"
  326. opentelemetry-lwt >= "0.6"
  327. opium >= "0.15.0"
  328. opium-graphql
  329. opium-testing
  330. opium_kernel
  331. orewa
  332. orgeat
  333. ortac-core
  334. osnap < "0.3.0"
  335. osx-acl
  336. osx-attr
  337. osx-cf
  338. osx-fsevents
  339. osx-membership
  340. osx-mount
  341. osx-xattr
  342. otoggl
  343. owl >= "0.6.0" & != "0.9.0" & != "1.0.0"
  344. owl-base < "0.5.0"
  345. owl-ode >= "0.1.0" & != "0.2.0"
  346. owl-symbolic
  347. passmaker
  348. patch
  349. pbkdf
  350. pecu >= "0.2"
  351. pf-qubes
  352. pg_query >= "0.9.6"
  353. pgx >= "1.0"
  354. pgx_unix >= "1.0"
  355. pgx_value_core
  356. pgx_value_ptime
  357. phylogenetics
  358. piaf
  359. polyglot
  360. polynomial
  361. ppx_blob >= "0.3.0"
  362. ppx_deriving_cmdliner
  363. ppx_deriving_qcheck
  364. ppx_deriving_rpc
  365. ppx_deriving_yaml
  366. ppx_graphql >= "0.2.0"
  367. ppx_inline_alcotest
  368. ppx_parser
  369. ppx_protocol_conv >= "5.0.0"
  370. ppx_protocol_conv_json >= "5.0.0"
  371. ppx_protocol_conv_jsonm >= "5.0.0"
  372. ppx_protocol_conv_msgpack >= "5.0.0"
  373. ppx_protocol_conv_xml_light >= "5.0.0"
  374. ppx_protocol_conv_xmlm
  375. ppx_protocol_conv_yaml >= "5.0.0"
  376. ppx_repr
  377. ppx_subliner
  378. ppx_units
  379. ppx_yojson >= "1.1.0"
  380. pratter
  381. prbnmcn-ucb1 >= "0.0.2"
  382. prc
  383. preface
  384. pretty_expressive
  385. prettym
  386. proc-smaps
  387. producer < "0.2.0"
  388. progress
  389. prom
  390. prometheus < "1.2"
  391. prometheus-app
  392. protocell
  393. protocol-9p >= "0.3" & < "0.11.0" | >= "0.11.2"
  394. protocol-9p-unix
  395. psq
  396. pyast
  397. qcheck >= "0.18"
  398. qcheck-alcotest
  399. qcheck-core >= "0.18"
  400. quickjs
  401. radis
  402. randii
  403. reason-standard
  404. reparse >= "2.0.0" & < "3.0.0"
  405. reparse-unix < "2.1.0"
  406. resp
  407. resp-unix >= "0.10.0"
  408. rfc1951 < "1.0.0"
  409. routes < "2.0.0"
  410. rpc >= "7.1.0"
  411. rpclib >= "7.1.0"
  412. rpclib-async
  413. rpclib-lwt >= "7.1.0"
  414. rubytt
  415. SZXX >= "4.0.0"
  416. salsa20
  417. salsa20-core
  418. sanddb >= "0.2"
  419. scaml >= "1.5.0"
  420. scrypt-kdf
  421. secp256k1 >= "0.4.1"
  422. secp256k1-internal
  423. semver >= "0.2.1"
  424. sendmail
  425. sendmail-lwt
  426. sendmsg
  427. server-reason-react
  428. session-cookie
  429. session-cookie-async
  430. session-cookie-lwt
  431. sherlodoc
  432. sihl < "0.2.0"
  433. sihl-type
  434. slug
  435. smol
  436. smol-helpers
  437. sodium-fmt
  438. solidity-alcotest
  439. spdx_licenses
  440. spectrum
  441. spin >= "0.7.0"
  442. squirrel
  443. ssh-agent
  444. ssl >= "0.6.0"
  445. stramon-lib
  446. syslog-rfc5424
  447. tcpip >= "2.4.2" & < "3.4.2" | >= "6.2.0" & < "8.0.0"
  448. tdigest < "2.1.0"
  449. terminal
  450. terminal_size >= "0.1.1"
  451. terminus
  452. terminus-cohttp
  453. terminus-hlc
  454. terml
  455. textmate-language >= "0.3.0"
  456. textrazor
  457. tezos-base-test-helpers < "13.0"
  458. tezos-bls12-381-polynomial
  459. tezos-client-base < "12.0"
  460. tezos-crypto >= "8.0" & < "9.0"
  461. tezos-lmdb
  462. tezos-plompiler = "0.1.3"
  463. tezos-plonk = "0.1.3"
  464. tezos-signer-backends >= "8.0" & < "13.0"
  465. tezos-stdlib >= "8.0" & < "12.0"
  466. tezos-test-helpers < "12.0"
  467. tftp
  468. timedesc
  469. timere
  470. timmy
  471. timmy-jsoo
  472. timmy-unix
  473. tls >= "0.12.0"
  474. toc
  475. topojson
  476. topojsone
  477. transept
  478. twostep
  479. type_eq
  480. type_id
  481. typebeat
  482. typeid >= "1.0.1"
  483. tyre >= "0.4"
  484. tyxml >= "4.0.0"
  485. tyxml-jsx
  486. tyxml-ppx >= "4.3.0"
  487. tyxml-syntax
  488. uecc
  489. ulid
  490. universal-portal
  491. unix-dirent
  492. unix-errno >= "0.3.0"
  493. unix-fcntl >= "0.3.0"
  494. unix-sys-resource
  495. unix-sys-stat
  496. unix-time
  497. unstrctrd
  498. uring < "0.4"
  499. user-agent-parser
  500. uspf
  501. uspf-lwt
  502. uspf-unix
  503. utop >= "2.13.0"
  504. validate
  505. validator
  506. vercel
  507. vpnkit
  508. wayland >= "2.0"
  509. websocketaf
  510. x509 >= "0.7.0"
  511. xapi-rrd >= "1.8.2"
  512. xapi-stdext-date
  513. xapi-stdext-encodings
  514. xapi-stdext-std >= "4.16.0"
  515. yaml < "3.2.0"
  516. yaml-sexp
  517. yocaml
  518. yocaml_yaml
  519. yojson >= "1.6.0"
  520. yuscii >= "0.3.0"
  521. yuujinchou = "1.0.0"
  522. zar
  523. zed >= "3.2.2"
  524. zlist < "0.4.0"

Conflicts

None