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. arc
  15. archetype >= "1.4.2"
  16. archi
  17. arp
  18. arrakis < "1.1.0"
  19. art < "0.3.0"
  20. asai
  21. asak >= "0.2"
  22. asli >= "0.2.0"
  23. asn1-combinators >= "0.2.5"
  24. atd >= "2.3.3"
  25. atdgen >= "2.10.0"
  26. atdpy
  27. atdts
  28. base32
  29. base64 >= "2.1.2" & < "3.2.0" | >= "3.4.0"
  30. bastet
  31. bastet_async
  32. bastet_lwt
  33. bech32
  34. bechamel >= "0.5.0"
  35. bigarray-overlap
  36. bigstringaf
  37. bitlib
  38. blake2
  39. bloomf
  40. bls12-381 < "0.4.1" | >= "3.0.0" & < "18.0"
  41. bls12-381-hash
  42. bls12-381-js >= "0.4.2"
  43. bls12-381-js-gen >= "0.4.2"
  44. bls12-381-legacy
  45. bls12-381-signature
  46. bls12-381-unix
  47. blurhash
  48. brisk-reconciler
  49. builder-web
  50. bytebuffer
  51. ca-certs
  52. ca-certs-nss
  53. cactus
  54. caldav
  55. calendar >= "3.0.0"
  56. calendars >= "2.0.0"
  57. callipyge
  58. camlix
  59. camlkit
  60. camlkit-base
  61. capnp-rpc < "1.2.3"
  62. capnp-rpc-unix < "1.2.3"
  63. caqti >= "1.7.0"
  64. caqti-async >= "1.7.0"
  65. caqti-driver-mariadb >= "1.7.0"
  66. caqti-driver-postgresql >= "1.7.0"
  67. caqti-driver-sqlite3 >= "1.7.0"
  68. caqti-dynload >= "2.0.1"
  69. caqti-eio
  70. caqti-lwt >= "1.7.0"
  71. caqti-miou
  72. carray
  73. carton < "1.0.0"
  74. carton-git
  75. carton-lwt >= "0.4.3" & < "1.0.0"
  76. catala >= "0.6.0"
  77. cborl
  78. cf-lwt
  79. chacha
  80. chamelon
  81. chamelon-unix
  82. charrua-client
  83. charrua-server
  84. checkseum >= "0.0.3"
  85. cid
  86. clarity-lang
  87. class_group_vdf
  88. cohttp < "6.0.0"
  89. cohttp-curl-async < "6.1.0"
  90. cohttp-eio = "6.0.0~beta2"
  91. colombe >= "0.2.0"
  92. color
  93. commons
  94. conan
  95. conan-cli
  96. conan-database
  97. conan-lwt
  98. conan-unix
  99. conex < "0.10.0"
  100. conex-mirage-crypto
  101. conformist
  102. cookie
  103. cow >= "2.2.0"
  104. crockford
  105. css
  106. css-parser
  107. cstruct
  108. cstruct-sexp
  109. ctypes-zarith
  110. cuid
  111. curly
  112. current
  113. current-albatross-deployer
  114. current_git >= "0.7.1"
  115. current_incr
  116. data-encoding
  117. dates_calc
  118. dbase4
  119. decimal >= "0.3.0"
  120. decompress < "1.5.3"
  121. depyt
  122. digestif >= "0.9.0"
  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. dmarc
  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 < "10.2.4"
  141. domain-name
  142. dream
  143. dream-pure
  144. duff
  145. dune-deps >= "1.4.0"
  146. dune-release >= "1.0.0"
  147. duration
  148. echo
  149. eio < "0.12"
  150. eio_linux < "0.12"
  151. eio_windows < "0.12"
  152. emile
  153. encore
  154. eqaf >= "0.5"
  155. equinoxe
  156. equinoxe-cohttp
  157. equinoxe-hlc
  158. ezgzip
  159. ezjsonm
  160. ezjsonm-lwt
  161. FPauth
  162. FPauth-core
  163. FPauth-responses
  164. FPauth-strategies
  165. faraday != "0.2.0"
  166. farfadet
  167. fat-filesystem
  168. fehu
  169. ff
  170. ff-pbt
  171. flex-array
  172. flux
  173. fluxt
  174. forester >= "5.0"
  175. fsevents-lwt
  176. functoria
  177. fungi
  178. geojson
  179. geoml >= "0.1.1"
  180. git
  181. git-cohttp
  182. git-cohttp-unix
  183. git-kv >= "0.2.0"
  184. git-mirage
  185. git-net
  186. git-split
  187. git-unix
  188. gitlab-unix
  189. glicko2
  190. gmap
  191. gobba
  192. gpt
  193. graphql
  194. graphql-async
  195. graphql-cohttp >= "0.13.0"
  196. graphql-lwt
  197. graphql_parser != "0.11.0"
  198. graphql_ppx
  199. h1
  200. h1_parser
  201. h2
  202. hacl
  203. hacl-star >= "0.6.0" & < "0.7.2"
  204. hacl_func
  205. hacl_x25519
  206. handlebars-ml >= "0.2.1"
  207. highlexer
  208. hkdf
  209. hockmd
  210. html_of_jsx
  211. http < "6.0.0"
  212. http-multipart-formdata < "2.0.0"
  213. httpaf >= "0.2.0"
  214. httpun
  215. httpun-ws
  216. hugin
  217. huml
  218. hvsock
  219. icalendar
  220. imagelib
  221. index
  222. inferno >= "20220603"
  223. influxdb-async
  224. influxdb-lwt
  225. inquire < "0.2.0"
  226. intel_hex >= "0.3"
  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" & < "1.1.1"
  246. json_decoder
  247. jsonfeed
  248. jsonxt
  249. junit_alcotest < "2.1.0"
  250. jwto
  251. kaun
  252. kdf
  253. ke >= "0.2"
  254. kkmarkdown
  255. kmt
  256. lambda-runtime
  257. lambda_streams
  258. lambda_streams_async
  259. lambdapi
  260. layoutz
  261. letters
  262. liquid_ml >= "0.1.3"
  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. neo4j_bolt
  318. nloge
  319. nocoiner
  320. non_empty_list
  321. nx
  322. nx-datasets
  323. nx-text
  324. OCADml >= "0.6.0"
  325. obatcher
  326. ocaml-index < "5.4.1-503"
  327. ocaml-r >= "0.4.0"
  328. ocaml-version >= "3.5.0"
  329. ocamlformat >= "0.13.0" & < "0.25.1"
  330. ocamlformat-lib
  331. ocamlformat-mlx-lib
  332. ocamlformat-rpc < "removed"
  333. ocamline
  334. ocluster < "0.3.0"
  335. ocue
  336. odoc < "2.1.1"
  337. oenv >= "0.1.0"
  338. ohex
  339. oidc
  340. opam-0install
  341. opam-0install-cudf >= "0.5.0"
  342. opam-compiler
  343. opam-file-format >= "2.1.1"
  344. opam-repomin
  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. passe
  375. passmaker
  376. patch < "3.0.0~alpha2"
  377. pbkdf
  378. pecu >= "0.2"
  379. pf-qubes
  380. pg_query >= "0.9.6"
  381. pgx >= "1.0"
  382. pgx_unix >= "1.0"
  383. pgx_value_core
  384. pgx_value_ptime
  385. phylogenetics
  386. piaf
  387. plebeia >= "2.0.0"
  388. polyglot
  389. polynomial
  390. ppx_blob >= "0.3.0"
  391. ppx_deriving_cmdliner
  392. ppx_deriving_ezjsonm
  393. ppx_deriving_qcheck
  394. ppx_deriving_rpc
  395. ppx_deriving_yaml
  396. ppx_inline_alcotest
  397. ppx_marshal
  398. ppx_parser
  399. ppx_protocol_conv >= "5.0.0"
  400. ppx_protocol_conv_json >= "5.0.0"
  401. ppx_protocol_conv_jsonm >= "5.0.0"
  402. ppx_protocol_conv_msgpack >= "5.0.0"
  403. ppx_protocol_conv_xml_light >= "5.0.0"
  404. ppx_protocol_conv_xmlm
  405. ppx_protocol_conv_yaml >= "5.0.0"
  406. ppx_repr
  407. ppx_subliner
  408. ppx_units
  409. ppx_yojson >= "1.1.0"
  410. pratter
  411. prbnmcn-ucb1 >= "0.0.2"
  412. prc
  413. preface
  414. pretty_expressive
  415. prettym
  416. proc-smaps
  417. producer < "0.2.0"
  418. progress
  419. prom
  420. prometheus < "1.2"
  421. prometheus-app
  422. protocell
  423. protocol-9p < "0.11.0" | >= "0.11.2"
  424. protocol-9p-unix
  425. proton
  426. psq
  427. public-suffix
  428. pyast
  429. qcaml
  430. qcheck >= "0.25"
  431. qcheck-alcotest
  432. qcheck-core >= "0.25"
  433. qcow-stream >= "0.13.0"
  434. qcow-tool >= "0.13.0"
  435. qcow-types >= "0.13.0"
  436. query-json
  437. quickjs
  438. quill
  439. randii
  440. reason-standard
  441. red-black-tree
  442. reparse >= "2.0.0" & < "3.0.0"
  443. reparse-unix < "2.1.0"
  444. resp
  445. resp-unix >= "0.10.0"
  446. resto >= "0.8"
  447. rfc1951 < "1.0.0"
  448. routes < "2.0.0"
  449. rpc
  450. rpclib
  451. rpclib-async
  452. rpclib-lwt
  453. rpmfile < "0.3.0"
  454. rpmfile-eio
  455. rpmfile-unix
  456. rune
  457. SZXX >= "4.0.0"
  458. saga
  459. salsa20
  460. salsa20-core
  461. sanddb >= "0.2"
  462. scrypt-kdf
  463. secp256k1 >= "0.4.1"
  464. secp256k1-internal
  465. semver >= "0.2.1"
  466. sendmail
  467. sendmail-lwt
  468. sendmail-miou-unix
  469. sendmail-mirage
  470. sendmsg
  471. seqes
  472. server-reason-react
  473. session-cookie
  474. session-cookie-async
  475. session-cookie-lwt
  476. shakuhachi
  477. sherlodoc
  478. sihl < "0.2.0"
  479. sihl-type
  480. slug
  481. smaws-clients
  482. smaws-lib
  483. smol
  484. smol-helpers
  485. sodium-fmt
  486. solidity-alcotest
  487. sowilo
  488. spdx_licenses
  489. spectrum >= "0.2.0"
  490. spin >= "0.7.0"
  491. spurs < "0.1.1"
  492. squirrel
  493. ssh-agent
  494. ssl >= "0.6.0"
  495. stramon-lib
  496. stringx
  497. styled-ppx
  498. swapfs
  499. synchronizer >= "0.2"
  500. syslog-rfc5424 < "0.2"
  501. talon
  502. tcpip
  503. tdigest < "2.1.0"
  504. term-indexing
  505. term-tools
  506. terminal
  507. terminal_size >= "0.1.1"
  508. terminus
  509. terminus-cohttp
  510. terminus-hlc
  511. terml
  512. testo
  513. testo-lwt
  514. textmate-language >= "0.3.0"
  515. textrazor
  516. timedesc
  517. timere
  518. timmy
  519. timmy-jsoo
  520. timmy-lwt
  521. timmy-unix
  522. tls >= "0.12.8"
  523. toc
  524. topojson
  525. topojsone
  526. traits
  527. transept
  528. tsort >= "2.2.0"
  529. twostep
  530. type_eq
  531. type_id
  532. typeid >= "1.0.1"
  533. tyre >= "0.4"
  534. tyxml >= "4.2.0"
  535. tyxml-jsx
  536. tyxml-ppx >= "4.3.0"
  537. tyxml-syntax
  538. uecc
  539. ulid
  540. universal-portal
  541. unix-dirent
  542. unix-errno
  543. unix-sys-resource
  544. unix-sys-stat
  545. unix-time
  546. unstrctrd
  547. uring < "0.4"
  548. user-agent-parser
  549. uspf
  550. uspf-lwt
  551. uspf-mirage
  552. uspf-unix
  553. utcp
  554. utop >= "2.13.0"
  555. validate
  556. validator
  557. vercel
  558. vhd-format-lwt >= "0.13.0"
  559. wayland >= "2.0"
  560. wcwidth
  561. websocketaf
  562. x509 >= "0.7.0"
  563. xapi-rrd
  564. xapi-stdext-date
  565. xapi-stdext-encodings
  566. xapi-stdext-std >= "4.16.0"
  567. yaml
  568. yaml-sexp
  569. yocaml
  570. yocaml_syndication >= "2.0.0"
  571. yocaml_yaml < "2.0.0"
  572. yojson >= "1.6.0"
  573. yojson-five
  574. yuscii >= "0.3.0"
  575. yuujinchou >= "1.0.0"
  576. zar
  577. zed >= "3.2.2"
  578. zlist < "0.4.0"

Conflicts (1)

  1. result < "1.5"