package alcotest

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

Install

Dune Dependency

Authors

Maintainers

Sources

alcotest-mirage-1.2.0.tbz
sha256=d82b7236081362320bd6dd968b192096bb68e3c688beab825fb0ebd3994daa87
sha512=67ec5355bb4f8cb806b8b0cecf4831c4ad20aa53ab161c8991bf46642c6d828a4e1de0aa2a16f880ebe528dbf7800e2a99778bf8397a9b7cb921301d0c0f4b70

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: 13 Jul 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.0"

Dev Dependencies

None

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

Conflicts

None