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

Conflicts (1)

  1. result < "1.5"