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

Conflicts (1)

  1. result < "1.5"