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. checked_oint < "0.1.1"
  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-albatross-deployer
  107. current_git >= "0.6.4"
  108. current_incr
  109. cwe_checker
  110. data-encoding
  111. datakit >= "0.12.0"
  112. datakit-bridge-github >= "0.12.0"
  113. datakit-ci
  114. datakit-client-git >= "0.12.0"
  115. decompress >= "0.8" & < "1.5.3"
  116. depyt
  117. digestif >= "0.8.1"
  118. dirsp-exchange-kbb2017
  119. dirsp-proscript-mirage
  120. dirsp-ps2ocaml
  121. dispatch >= "0.4.1"
  122. dkim
  123. dkim-bin
  124. dkim-mirage
  125. dkml-install
  126. dkml-install-installer
  127. dkml-install-runner
  128. dkml-package-console
  129. dns >= "4.0.0"
  130. dns-cli
  131. dns-client >= "4.6.0"
  132. dns-forward < "0.9.0"
  133. dns-forward-lwt-unix
  134. dns-resolver
  135. dns-server
  136. dns-tsig
  137. dnssd
  138. dnssec
  139. docfd >= "2.2.0"
  140. dog < "0.2.1"
  141. domain-name
  142. dream
  143. dream-pure
  144. duff
  145. dune-release >= "1.0.0"
  146. duration >= "0.1.1"
  147. eio < "0.12"
  148. eio_linux < "0.12"
  149. eio_windows < "0.12"
  150. emile
  151. encore
  152. eqaf >= "0.5"
  153. equinoxe
  154. equinoxe-cohttp
  155. equinoxe-hlc
  156. eris
  157. eris-lwt
  158. ezgzip
  159. ezjsonm >= "0.4.2" & < "1.3.0"
  160. ezjsonm-lwt
  161. FPauth
  162. FPauth-core
  163. FPauth-responses
  164. FPauth-strategies
  165. faraday != "0.2.0"
  166. farfadet
  167. fat-filesystem >= "0.12.0"
  168. ff
  169. ff-pbt
  170. flex-array
  171. fsevents-lwt
  172. functoria >= "2.2.0"
  173. functoria-runtime >= "2.2.0" & < "3.0.1" | = "3.1.2"
  174. geojson
  175. geoml >= "0.1.1"
  176. git = "1.4.10" | = "1.5.0" | >= "1.5.2" & != "1.10.0"
  177. git-cohttp
  178. git-cohttp-mirage
  179. git-cohttp-unix
  180. git-mirage
  181. git-unix >= "1.10.0" & != "2.1.0"
  182. gitlab-unix
  183. glicko2
  184. gmap >= "0.3.0"
  185. gobba
  186. gpt
  187. graphql
  188. graphql-async
  189. graphql-cohttp >= "0.13.0"
  190. graphql-lwt
  191. graphql_parser != "0.11.0"
  192. graphql_ppx >= "0.7.1"
  193. h1_parser
  194. h2
  195. hacl
  196. hacl-star >= "0.6.0"
  197. hacl_func
  198. hacl_x25519 >= "0.2.0"
  199. highlexer
  200. hkdf
  201. hockmd
  202. html_of_jsx
  203. http
  204. http-multipart-formdata < "2.0.0"
  205. httpaf >= "0.2.0"
  206. hvsock
  207. icalendar >= "0.1.4"
  208. imagelib >= "20200929"
  209. index
  210. inferno >= "20220603"
  211. influxdb-async
  212. influxdb-lwt
  213. inquire < "0.2.0"
  214. interval-map
  215. iomux
  216. irmin < "0.8.0" | >= "0.9.6" & != "0.11.1" & < "1.0.0" | >= "2.0.0" & != "2.3.0"
  217. irmin-bench >= "2.7.0"
  218. irmin-chunk < "1.3.0" | >= "2.3.0"
  219. irmin-cli
  220. irmin-containers
  221. irmin-fs < "1.3.0" | >= "2.3.0"
  222. irmin-git < "2.0.0" | >= "2.3.0"
  223. irmin-graphql >= "2.3.0"
  224. irmin-http < "2.0.0"
  225. irmin-mem < "1.3.0" | >= "2.3.0"
  226. irmin-pack >= "2.4.0" & != "2.6.1"
  227. irmin-pack-tools
  228. irmin-test >= "2.2.0" & < "3.0.0"
  229. irmin-tezos
  230. irmin-tezos-utils
  231. irmin-unix >= "1.0.0" & < "1.3.3" | >= "2.4.0" & != "2.6.1"
  232. irmin-watcher != "0.3.0"
  233. jekyll-format
  234. jerboa
  235. jitsu
  236. jose
  237. json-data-encoding >= "0.9"
  238. json_decoder
  239. jsonxt
  240. junit_alcotest
  241. jwto
  242. ke >= "0.2"
  243. kkmarkdown
  244. lambda-runtime
  245. lambda_streams
  246. lambda_streams_async
  247. lambdapi >= "2.0.0"
  248. lambdoc >= "1.0-beta4"
  249. ledgerwallet-tezos >= "0.2.1" & < "0.4.0"
  250. letters
  251. lmdb >= "1.0"
  252. logical
  253. logtk >= "1.6"
  254. lp
  255. lp-glpk
  256. lp-glpk-js
  257. lp-gurobi
  258. lru
  259. lt-code
  260. luv
  261. mbr-format >= "1.0.0"
  262. mdx >= "1.6.0"
  263. mec
  264. mechaml = "1.0.0" | >= "1.2.1"
  265. merge-queues >= "0.2.0"
  266. merge-ropes >= "0.2.0"
  267. metrics
  268. middleware
  269. mimic
  270. minicaml = "0.3.1" | >= "0.4"
  271. mirage >= "4.0.0~beta1"
  272. mirage-block-partition
  273. mirage-block-ramdisk = "0.3"
  274. mirage-channel >= "4.0.0"
  275. mirage-channel-lwt < "3.1.0"
  276. mirage-crypto-ec != "0.9.2"
  277. mirage-flow >= "1.0.2" & < "1.2.0"
  278. mirage-flow-unix != "1.3.0" & < "1.5.0" | = "2.0.0" | >= "3.0.0"
  279. mirage-fs-mem
  280. mirage-fs-unix >= "1.2.0" & < "1.4.1"
  281. mirage-kv >= "2.0.0"
  282. mirage-kv-mem
  283. mirage-kv-unix >= "3.0.0"
  284. mirage-logs >= "0.3.0"
  285. mirage-nat
  286. mirage-net-unix >= "2.3.0"
  287. mirage-runtime >= "4.0.0~beta1" & < "4.5.0"
  288. mirage-tc
  289. mjson
  290. mmdb < "0.3.0"
  291. mnd
  292. monocypher
  293. mrmime >= "0.2.0"
  294. mrt-format
  295. msgpck >= "1.6"
  296. mssql >= "2.0.3"
  297. multibase
  298. multihash
  299. multihash-digestif
  300. multipart-form-data
  301. multipart_form
  302. multipart_form-eio
  303. multipart_form-lwt
  304. named-pipe
  305. nanoid
  306. nbd >= "4.0.3"
  307. nbd-tool
  308. nloge
  309. nocoiner
  310. non_empty_list
  311. OCADml >= "0.6.0"
  312. ocaml-r >= "0.4.0"
  313. ocaml-version >= "3.1.0"
  314. ocamlformat >= "0.13.0" & != "0.19.0~4.13preview" & < "0.25.1"
  315. ocamlformat-lib
  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-compiler
  324. opam-file-format >= "2.1.1"
  325. opentelemetry >= "0.6"
  326. opentelemetry-client-cohttp-lwt >= "0.6"
  327. opentelemetry-client-ocurl >= "0.6"
  328. opentelemetry-cohttp-lwt >= "0.6"
  329. opentelemetry-lwt >= "0.6"
  330. opium >= "0.15.0"
  331. opium-graphql
  332. opium-testing
  333. opium_kernel
  334. orewa
  335. orgeat
  336. ortac-core
  337. osnap < "0.3.0"
  338. osx-acl
  339. osx-attr
  340. osx-cf
  341. osx-fsevents
  342. osx-membership
  343. osx-mount
  344. osx-xattr
  345. otoggl
  346. owl >= "0.6.0" & != "0.9.0" & != "1.0.0"
  347. owl-base < "0.5.0"
  348. owl-ode >= "0.1.0" & != "0.2.0"
  349. owl-symbolic
  350. passmaker
  351. patch
  352. pbkdf
  353. pecu >= "0.2"
  354. pf-qubes
  355. pg_query >= "0.9.6"
  356. pgx >= "1.0"
  357. pgx_unix >= "1.0"
  358. pgx_value_core
  359. pgx_value_ptime
  360. phylogenetics
  361. piaf
  362. polyglot
  363. polynomial
  364. ppx_blob >= "0.3.0"
  365. ppx_deriving_cmdliner
  366. ppx_deriving_qcheck
  367. ppx_deriving_rpc
  368. ppx_deriving_yaml
  369. ppx_graphql >= "0.2.0"
  370. ppx_inline_alcotest
  371. ppx_parser
  372. ppx_protocol_conv >= "5.0.0"
  373. ppx_protocol_conv_json >= "5.0.0"
  374. ppx_protocol_conv_jsonm >= "5.0.0"
  375. ppx_protocol_conv_msgpack >= "5.0.0"
  376. ppx_protocol_conv_xml_light >= "5.0.0"
  377. ppx_protocol_conv_xmlm
  378. ppx_protocol_conv_yaml >= "5.0.0"
  379. ppx_repr
  380. ppx_subliner
  381. ppx_units
  382. ppx_yojson >= "1.1.0"
  383. pratter
  384. prbnmcn-ucb1 >= "0.0.2"
  385. prc
  386. preface
  387. pretty_expressive
  388. prettym
  389. proc-smaps
  390. producer < "0.2.0"
  391. progress
  392. prom
  393. prometheus < "1.2"
  394. prometheus-app
  395. protocell
  396. protocol-9p >= "0.3" & < "0.11.0" | >= "0.11.2"
  397. protocol-9p-unix
  398. psq
  399. pyast
  400. qcheck >= "0.18"
  401. qcheck-alcotest
  402. qcheck-core >= "0.18"
  403. quickjs
  404. radis
  405. randii
  406. reason-standard
  407. reparse >= "2.0.0" & < "3.0.0"
  408. reparse-unix < "2.1.0"
  409. resp
  410. resp-unix >= "0.10.0"
  411. rfc1951 < "1.0.0"
  412. routes < "2.0.0"
  413. rpc >= "7.1.0"
  414. rpclib >= "7.1.0"
  415. rpclib-async
  416. rpclib-lwt >= "7.1.0"
  417. rubytt
  418. SZXX >= "4.0.0"
  419. salsa20
  420. salsa20-core
  421. sanddb >= "0.2"
  422. scaml >= "1.5.0"
  423. scrypt-kdf
  424. secp256k1 >= "0.4.1"
  425. secp256k1-internal
  426. semver >= "0.2.1"
  427. sendmail
  428. sendmail-lwt
  429. sendmsg
  430. server-reason-react
  431. session-cookie
  432. session-cookie-async
  433. session-cookie-lwt
  434. sherlodoc
  435. sihl < "0.2.0"
  436. sihl-type
  437. slug
  438. smol
  439. smol-helpers
  440. sodium-fmt
  441. solidity-alcotest
  442. spdx_licenses
  443. spectrum
  444. spin >= "0.7.0"
  445. squirrel
  446. ssh-agent
  447. ssl >= "0.6.0"
  448. stramon-lib
  449. styled-ppx
  450. syslog-rfc5424
  451. tcpip >= "2.4.2" & < "3.4.2" | >= "6.2.0" & < "8.0.0"
  452. tdigest < "2.1.0"
  453. terminal
  454. terminal_size >= "0.1.1"
  455. terminus
  456. terminus-cohttp
  457. terminus-hlc
  458. terml
  459. textmate-language >= "0.3.0"
  460. textrazor
  461. tezos-base-test-helpers < "13.0"
  462. tezos-bls12-381-polynomial
  463. tezos-client-base < "12.0"
  464. tezos-crypto >= "8.0" & < "9.0"
  465. tezos-lmdb
  466. tezos-plompiler = "0.1.3"
  467. tezos-plonk = "0.1.3"
  468. tezos-signer-backends >= "8.0" & < "13.0"
  469. tezos-stdlib >= "8.0" & < "12.0"
  470. tezos-test-helpers < "12.0"
  471. tftp
  472. timedesc
  473. timere
  474. timmy
  475. timmy-jsoo
  476. timmy-unix
  477. tls >= "0.12.0"
  478. toc
  479. topojson
  480. topojsone
  481. transept
  482. twostep
  483. type_eq
  484. type_id
  485. typebeat
  486. typeid >= "1.0.1"
  487. tyre >= "0.4"
  488. tyxml >= "4.0.0"
  489. tyxml-jsx
  490. tyxml-ppx >= "4.3.0"
  491. tyxml-syntax
  492. uecc
  493. ulid
  494. universal-portal
  495. unix-dirent
  496. unix-errno >= "0.3.0"
  497. unix-fcntl >= "0.3.0"
  498. unix-sys-resource
  499. unix-sys-stat
  500. unix-time
  501. unstrctrd
  502. uring < "0.4"
  503. user-agent-parser
  504. uspf
  505. uspf-lwt
  506. uspf-unix
  507. utop >= "2.13.0"
  508. validate
  509. validator
  510. vercel
  511. vpnkit
  512. wayland >= "2.0"
  513. wcwidth
  514. websocketaf
  515. x509 >= "0.7.0"
  516. xapi-rrd >= "1.8.2"
  517. xapi-stdext-date
  518. xapi-stdext-encodings
  519. xapi-stdext-std >= "4.16.0"
  520. yaml < "3.2.0"
  521. yaml-sexp
  522. yocaml
  523. yocaml_yaml
  524. yojson >= "1.6.0"
  525. yuscii >= "0.3.0"
  526. yuujinchou = "1.0.0"
  527. zar
  528. zed >= "3.2.2"
  529. zlist < "0.4.0"

Conflicts

None

OCaml

Innovation. Community. Security.