package alcotest

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

alcotest-js-1.5.0.tbz
sha256=54281907e02d78995df246dc2e10ed182828294ad2059347a1e3a13354848f6c
sha512=1aea91de40795ec4f6603d510107e4b663c1a94bd223f162ad231316d8595e9e098cabbe28a46bdcb588942f3d103d8377373d533bcc7413ba3868a577469b45

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: 12 Oct 2021

README

A lightweight and colourful test framework.


Alcotest exposes a 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.

The API documentation can be found [here][docs]. For information on contributing to Alcotest, see CONTRIBUTING.md.

OCaml-CI Build Status docs


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. ocaml-syntax-shims
  2. uutf >= "1.0.1"
  3. stdlib-shims
  4. re >= "1.7.2"
  5. cmdliner >= "1.0.0" & < "2.0.0"
  6. astring
  7. fmt >= "0.8.7"
  8. ocaml >= "4.03.0"
  9. dune >= "2.8"

Dev Dependencies (2)

  1. odoc with-doc
  2. cmdliner with-test & < "1.1.0"

  1. ahrocksdb
  2. albatross >= "1.5.4"
  3. alcotest-async < "1.7.0"
  4. alg_structs_qcheck
  5. algaeff
  6. ambient-context
  7. ambient-context-eio
  8. ambient-context-lwt
  9. angstrom >= "0.7.0"
  10. ansi >= "0.6.0"
  11. anycache >= "0.7.4"
  12. anycache-async
  13. anycache-lwt
  14. archetype >= "1.4.2"
  15. archi
  16. arp
  17. arrakis < "1.1.0"
  18. art
  19. asai
  20. asak >= "0.2"
  21. asli >= "0.2.0"
  22. asn1-combinators >= "0.2.5"
  23. atd >= "2.3.3"
  24. atdgen >= "2.10.0"
  25. atdpy
  26. atdts
  27. base32
  28. base64 >= "2.1.2" & < "3.2.0" | >= "3.4.0"
  29. bastet
  30. bastet_async
  31. bastet_lwt
  32. bech32
  33. bechamel >= "0.5.0"
  34. bigarray-overlap
  35. bigstringaf
  36. bitlib
  37. blake2
  38. bloomf
  39. bls12-381 < "0.4.1" | >= "3.0.0" & < "18.0"
  40. bls12-381-hash
  41. bls12-381-js >= "0.4.2"
  42. bls12-381-js-gen >= "0.4.2"
  43. bls12-381-legacy
  44. bls12-381-signature
  45. bls12-381-unix
  46. blurhash
  47. brisk-reconciler
  48. builder-web
  49. bytebuffer
  50. ca-certs
  51. ca-certs-nss
  52. cactus
  53. caldav
  54. calendar >= "3.0.0"
  55. callipyge
  56. camlix
  57. camlkit
  58. camlkit-base
  59. capnp-rpc < "1.2.3"
  60. capnp-rpc-unix < "1.2.3"
  61. caqti >= "1.7.0"
  62. caqti-async >= "1.7.0"
  63. caqti-driver-mariadb >= "1.7.0"
  64. caqti-driver-postgresql >= "1.7.0"
  65. caqti-driver-sqlite3 >= "1.7.0"
  66. caqti-dynload >= "2.0.1"
  67. caqti-eio
  68. caqti-lwt >= "1.7.0"
  69. caqti-miou
  70. carray
  71. carton < "1.0.0"
  72. carton-git
  73. carton-lwt >= "0.4.3" & < "1.0.0"
  74. catala >= "0.6.0"
  75. cborl
  76. cf-lwt
  77. chacha
  78. chamelon
  79. chamelon-unix
  80. charrua-client
  81. charrua-server
  82. checkseum >= "0.0.3"
  83. cid
  84. clarity-lang
  85. class_group_vdf
  86. cohttp < "6.0.0"
  87. cohttp-curl-async < "6.1.0"
  88. cohttp-eio = "6.0.0~beta2"
  89. colombe >= "0.2.0"
  90. color
  91. commons
  92. conan
  93. conan-cli
  94. conan-database
  95. conan-lwt
  96. conan-unix
  97. conex < "0.10.0"
  98. conex-mirage-crypto
  99. conformist
  100. cookie
  101. cow >= "2.2.0"
  102. css
  103. css-parser
  104. cstruct
  105. cstruct-sexp
  106. ctypes-zarith
  107. cuid
  108. curly
  109. current
  110. current-albatross-deployer
  111. current_git >= "0.7.1"
  112. current_incr
  113. data-encoding
  114. dates_calc
  115. dbase4
  116. decimal >= "0.3.0"
  117. decompress < "1.5.3"
  118. depyt
  119. digestif >= "0.9.0"
  120. dirsp-exchange-kbb2017
  121. dirsp-proscript-mirage
  122. dirsp-ps2ocaml
  123. dispatch >= "0.4.1"
  124. dkim
  125. dkim-bin
  126. dkim-mirage
  127. dkml-dune-dsl-show
  128. dkml-install
  129. dkml-install-installer
  130. dkml-install-runner
  131. dkml-package-console
  132. dns >= "4.4.1"
  133. dns-cli
  134. dns-client >= "4.6.3"
  135. dns-forward-lwt-unix
  136. dns-resolver
  137. dns-server
  138. dns-tsig
  139. dnssd
  140. dnssec
  141. docfd >= "2.2.0"
  142. domain-name
  143. dream
  144. dream-pure
  145. duff
  146. dune-deps >= "1.4.0"
  147. dune-release >= "1.0.0"
  148. duration
  149. echo
  150. eio < "0.12"
  151. eio_linux < "0.12"
  152. eio_windows < "0.12"
  153. emile
  154. encore
  155. eqaf >= "0.5"
  156. equinoxe
  157. equinoxe-cohttp
  158. equinoxe-hlc
  159. ezgzip
  160. ezjsonm
  161. ezjsonm-lwt
  162. FPauth
  163. FPauth-core
  164. FPauth-responses
  165. FPauth-strategies
  166. faraday != "0.2.0"
  167. farfadet
  168. fat-filesystem
  169. fehu
  170. ff
  171. ff-pbt
  172. flex-array
  173. flux
  174. fluxt
  175. forester >= "5.0"
  176. fsevents-lwt
  177. functoria
  178. fungi
  179. geojson
  180. geoml >= "0.1.1"
  181. git
  182. git-cohttp
  183. git-cohttp-unix
  184. git-kv >= "0.2.0"
  185. git-mirage
  186. git-net
  187. git-split
  188. git-unix
  189. gitlab-unix
  190. glicko2
  191. gmap
  192. gobba
  193. gpt
  194. graphql
  195. graphql-async
  196. graphql-cohttp >= "0.13.0"
  197. graphql-lwt
  198. graphql_parser != "0.11.0"
  199. graphql_ppx
  200. h1
  201. h1_parser
  202. h2
  203. hacl
  204. hacl-star >= "0.6.0" & < "0.7.2"
  205. hacl_func
  206. hacl_x25519
  207. handlebars-ml >= "0.2.1"
  208. highlexer
  209. hkdf
  210. hockmd
  211. html_of_jsx
  212. http < "6.0.0"
  213. http-multipart-formdata < "2.0.0"
  214. httpaf >= "0.2.0"
  215. httpun
  216. httpun-ws
  217. hugin
  218. huml
  219. hvsock
  220. icalendar
  221. imagelib
  222. index
  223. inferno >= "20220603"
  224. influxdb-async
  225. influxdb-lwt
  226. inquire < "0.2.0"
  227. interval-map
  228. iomux
  229. irmin
  230. irmin-bench
  231. irmin-chunk
  232. irmin-cli
  233. irmin-containers
  234. irmin-fs
  235. irmin-git
  236. irmin-graphql
  237. irmin-pack
  238. irmin-pack-tools
  239. irmin-test < "3.6.1"
  240. irmin-tezos
  241. irmin-unix
  242. irmin-watcher
  243. jekyll-format
  244. jose
  245. json-data-encoding >= "0.9"
  246. json_decoder
  247. jsonxt
  248. junit_alcotest < "2.1.0"
  249. jwto
  250. kaun
  251. kdf
  252. ke >= "0.2"
  253. kkmarkdown
  254. kmt
  255. lambda-runtime
  256. lambda_streams
  257. lambda_streams_async
  258. lambdapi
  259. ledgerwallet-tezos >= "0.2.1" & < "0.4.0"
  260. letters
  261. lmdb >= "1.0"
  262. logical
  263. logtk >= "1.6"
  264. lp
  265. lp-glpk
  266. lp-glpk-js < "0.5.0"
  267. lp-gurobi < "0.5.0"
  268. lru
  269. lt-code
  270. luv
  271. mbr-format
  272. mdx >= "1.6.0"
  273. mec
  274. mechaml >= "1.2.1"
  275. merlin = "4.17.1-501"
  276. merlin-lib >= "4.17.1-501"
  277. metrics
  278. middleware
  279. mimic
  280. minicaml = "0.3.1" | >= "0.4"
  281. mirage >= "4.0.0"
  282. mirage-block-partition
  283. mirage-block-ramdisk
  284. mirage-channel >= "4.0.1"
  285. mirage-crypto-ec
  286. mirage-flow-unix
  287. mirage-kv >= "2.0.0"
  288. mirage-kv-mem
  289. mirage-kv-unix >= "3.0.0"
  290. mirage-logs
  291. mirage-nat
  292. mirage-net-unix
  293. mirage-runtime < "4.7.0"
  294. mirage-tc
  295. mjson
  296. mlgpx
  297. mmdb < "0.3.0"
  298. mnd
  299. mqtt
  300. mrmime >= "0.2.0"
  301. msgpck >= "1.6"
  302. mssql >= "2.0.3"
  303. multibase
  304. multihash
  305. multihash-digestif
  306. multipart-form-data
  307. multipart_form
  308. multipart_form-eio
  309. multipart_form-lwt
  310. multipart_form-miou
  311. named-pipe
  312. nanoid
  313. nbd >= "4.0.3"
  314. nbd-tool
  315. nloge
  316. nocoiner
  317. non_empty_list
  318. nx
  319. nx-datasets
  320. nx-text
  321. OCADml >= "0.6.0"
  322. obatcher
  323. ocaml-index < "5.4.1-503"
  324. ocaml-r >= "0.4.0"
  325. ocaml-version >= "3.5.0"
  326. ocamlformat >= "0.13.0" & < "0.25.1"
  327. ocamlformat-lib
  328. ocamlformat-mlx-lib
  329. ocamlformat-rpc < "removed"
  330. ocamline
  331. ocluster < "0.3.0"
  332. octez-bls12-381-hash
  333. octez-bls12-381-signature
  334. octez-libs
  335. octez-mec
  336. ocue
  337. odoc < "2.1.1"
  338. oenv >= "0.1.0"
  339. ohex
  340. oidc
  341. opam-0install
  342. opam-0install-cudf >= "0.5.0"
  343. opam-compiler
  344. opam-file-format >= "2.1.1"
  345. opencage
  346. opentelemetry >= "0.6"
  347. opentelemetry-client-cohttp-eio
  348. opentelemetry-client-cohttp-lwt >= "0.6"
  349. opentelemetry-client-ocurl >= "0.6"
  350. opentelemetry-cohttp-lwt >= "0.6"
  351. opentelemetry-logs
  352. opentelemetry-lwt >= "0.6"
  353. opium
  354. opium-graphql
  355. opium-testing
  356. opium_kernel
  357. orewa
  358. orgeat
  359. ortac-core
  360. ortac-wrapper
  361. osnap < "0.3.0"
  362. osx-acl
  363. osx-attr
  364. osx-cf
  365. osx-fsevents
  366. osx-membership
  367. osx-mount
  368. osx-xattr
  369. otoggl
  370. owl >= "0.7.0" & != "0.9.0" & != "1.0.0"
  371. owl-base < "0.5.0"
  372. owl-ode >= "0.1.0" & != "0.2.0"
  373. owl-symbolic
  374. passmaker
  375. patch < "3.0.0~alpha2"
  376. pbkdf
  377. pecu >= "0.2"
  378. pf-qubes
  379. pg_query >= "0.9.6"
  380. pgx >= "1.0"
  381. pgx_unix >= "1.0"
  382. pgx_value_core
  383. pgx_value_ptime
  384. phylogenetics
  385. piaf
  386. plebeia >= "2.0.0"
  387. polyglot
  388. polynomial
  389. ppx_blob >= "0.3.0"
  390. ppx_deriving_cmdliner
  391. ppx_deriving_ezjsonm
  392. ppx_deriving_qcheck
  393. ppx_deriving_rpc
  394. ppx_deriving_yaml
  395. ppx_inline_alcotest
  396. ppx_marshal
  397. ppx_parser
  398. ppx_protocol_conv >= "5.0.0"
  399. ppx_protocol_conv_json >= "5.0.0"
  400. ppx_protocol_conv_jsonm >= "5.0.0"
  401. ppx_protocol_conv_msgpack >= "5.0.0"
  402. ppx_protocol_conv_xml_light >= "5.0.0"
  403. ppx_protocol_conv_xmlm
  404. ppx_protocol_conv_yaml >= "5.0.0"
  405. ppx_repr
  406. ppx_subliner
  407. ppx_units
  408. ppx_yojson >= "1.1.0"
  409. pratter
  410. prbnmcn-ucb1 >= "0.0.2"
  411. prc
  412. preface
  413. pretty_expressive
  414. prettym
  415. proc-smaps
  416. producer < "0.2.0"
  417. progress
  418. prom
  419. prometheus < "1.2"
  420. prometheus-app
  421. protocell
  422. protocol-9p < "0.11.0" | >= "0.11.2"
  423. protocol-9p-unix
  424. proton
  425. psq
  426. pyast
  427. qcheck >= "0.25"
  428. qcheck-alcotest
  429. qcheck-core >= "0.25"
  430. quickjs
  431. quill
  432. randii
  433. reason-standard
  434. red-black-tree
  435. reparse >= "2.0.0" & < "3.0.0"
  436. reparse-unix < "2.1.0"
  437. resp
  438. resp-unix >= "0.10.0"
  439. resto >= "0.8"
  440. rfc1951 < "1.0.0"
  441. routes < "2.0.0"
  442. rpc
  443. rpclib
  444. rpclib-async
  445. rpclib-lwt
  446. rpmfile < "0.3.0"
  447. rpmfile-eio
  448. rpmfile-unix
  449. rune
  450. SZXX >= "4.0.0"
  451. saga
  452. salsa20
  453. salsa20-core
  454. sanddb >= "0.2"
  455. scrypt-kdf
  456. secp256k1 >= "0.4.1"
  457. secp256k1-internal
  458. semver >= "0.2.1"
  459. sendmail
  460. sendmail-lwt
  461. sendmail-miou-unix
  462. sendmail-mirage
  463. sendmsg
  464. seqes
  465. server-reason-react
  466. session-cookie
  467. session-cookie-async
  468. session-cookie-lwt
  469. sherlodoc
  470. sihl < "0.2.0"
  471. sihl-type
  472. slug
  473. smaws-clients
  474. smaws-lib
  475. smol
  476. smol-helpers
  477. sodium-fmt
  478. solidity-alcotest
  479. sowilo
  480. spdx_licenses
  481. spectrum >= "0.2.0"
  482. spin >= "0.7.0"
  483. spurs
  484. squirrel
  485. ssh-agent
  486. ssl >= "0.6.0"
  487. stramon-lib
  488. stringx
  489. styled-ppx
  490. swapfs
  491. syslog-rfc5424
  492. talon
  493. tcpip
  494. tdigest < "2.1.0"
  495. term-indexing
  496. term-tools
  497. terminal
  498. terminal_size >= "0.1.1"
  499. terminus
  500. terminus-cohttp
  501. terminus-hlc
  502. terml
  503. testo
  504. testo-lwt
  505. textmate-language >= "0.3.0"
  506. textrazor
  507. tezos-base-test-helpers < "17.3"
  508. tezos-bls12-381-polynomial
  509. tezos-client-base < "17.3"
  510. tezos-client-base-unix < "17.3"
  511. tezos-crypto >= "16.0" & < "17.3"
  512. tezos-crypto-dal < "17.3"
  513. tezos-error-monad >= "12.3" & < "17.3"
  514. tezos-event-logging-test-helpers < "17.3"
  515. tezos-plompiler = "0.1.3"
  516. tezos-plonk = "0.1.3"
  517. tezos-shell-services >= "16.0" & < "17.3"
  518. tezos-stdlib != "12.3" & < "17.3"
  519. tezos-test-helpers < "17.3"
  520. tezos-version >= "16.0" & < "17.3"
  521. tezos-webassembly-interpreter < "17.3"
  522. timedesc
  523. timere
  524. timmy
  525. timmy-jsoo
  526. timmy-lwt
  527. timmy-unix
  528. tls >= "0.12.8"
  529. toc
  530. topojson
  531. topojsone
  532. traits
  533. transept
  534. tsort >= "2.2.0"
  535. twostep
  536. type_eq
  537. type_id
  538. typeid >= "1.0.1"
  539. tyre >= "0.4"
  540. tyxml >= "4.2.0"
  541. tyxml-jsx
  542. tyxml-ppx >= "4.3.0"
  543. tyxml-syntax
  544. uecc
  545. ulid
  546. universal-portal
  547. unix-dirent
  548. unix-errno
  549. unix-sys-resource
  550. unix-sys-stat
  551. unix-time
  552. unstrctrd
  553. uring < "0.4"
  554. user-agent-parser
  555. uspf
  556. uspf-lwt
  557. uspf-mirage
  558. uspf-unix
  559. utop >= "2.13.0"
  560. validate
  561. validator
  562. vercel
  563. vhd-format-lwt >= "0.13.0"
  564. vpnkit
  565. wayland >= "2.0"
  566. wcwidth
  567. websocketaf
  568. x509 >= "0.7.0"
  569. xapi-rrd
  570. xapi-stdext-date
  571. xapi-stdext-encodings
  572. xapi-stdext-std >= "4.16.0"
  573. yaml
  574. yaml-sexp
  575. yocaml
  576. yocaml_syndication >= "2.0.0"
  577. yocaml_yaml < "2.0.0"
  578. yojson >= "1.6.0"
  579. yojson-five
  580. yuscii >= "0.3.0"
  581. yuujinchou >= "1.0.0"
  582. zar
  583. zed >= "3.2.2"
  584. zlist < "0.4.0"

Conflicts (1)

  1. result < "1.5"