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

Conflicts

None