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

Conflicts (1)

  1. result < "1.5"