package alcotest

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

Install

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

Conflicts (1)

  1. result < "1.5"
OCaml

Innovation. Community. Security.