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. biotk >= "0.4"
  38. bitlib
  39. blake2
  40. bloomf
  41. bls12-381 < "0.4.1" | >= "3.0.0" & < "18.0"
  42. bls12-381-hash
  43. bls12-381-js >= "0.4.2"
  44. bls12-381-js-gen >= "0.4.2"
  45. bls12-381-legacy
  46. bls12-381-signature
  47. bls12-381-unix
  48. blurhash
  49. brisk-reconciler
  50. builder-web
  51. bytebuffer
  52. ca-certs
  53. ca-certs-nss
  54. cactus
  55. caldav
  56. calendar >= "3.0.0"
  57. calendars >= "2.0.0"
  58. callipyge
  59. camlix
  60. camlkit
  61. camlkit-base
  62. capnp-rpc < "1.2.3"
  63. capnp-rpc-unix < "1.2.3"
  64. caqti >= "1.7.0"
  65. caqti-async >= "1.7.0"
  66. caqti-driver-mariadb >= "1.7.0"
  67. caqti-driver-postgresql >= "1.7.0"
  68. caqti-driver-sqlite3 >= "1.7.0"
  69. caqti-dynload >= "2.0.1"
  70. caqti-eio
  71. caqti-lwt >= "1.7.0"
  72. caqti-miou
  73. carray
  74. carton < "1.0.0"
  75. carton-git
  76. carton-lwt >= "0.4.3" & < "1.0.0"
  77. catala >= "0.6.0"
  78. cborl
  79. cf-lwt
  80. chacha
  81. chamelon
  82. chamelon-unix
  83. charrua-client
  84. charrua-server
  85. checkseum >= "0.0.3"
  86. cid
  87. clarity-lang
  88. class_group_vdf
  89. cohttp < "6.0.0"
  90. cohttp-curl-async < "6.1.0"
  91. cohttp-eio = "6.0.0~beta2"
  92. colombe >= "0.2.0"
  93. color
  94. commons
  95. conan
  96. conan-cli
  97. conan-database
  98. conan-lwt
  99. conan-unix
  100. conex < "0.10.0"
  101. conex-mirage-crypto
  102. conformist
  103. cookie
  104. cow >= "2.2.0"
  105. crockford
  106. css
  107. css-parser
  108. cstruct
  109. cstruct-sexp
  110. ctypes-zarith
  111. cuid
  112. curly
  113. current
  114. current-albatross-deployer
  115. current_git >= "0.7.1"
  116. current_incr
  117. data-encoding
  118. dates_calc
  119. dbase4
  120. decimal >= "0.3.0"
  121. decompress < "1.5.3"
  122. depyt
  123. digestif >= "0.9.0"
  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. dmarc
  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 < "10.2.4"
  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. flux
  174. fluxt
  175. forester >= "5.0"
  176. fsevents-lwt
  177. functoria
  178. fungi
  179. geojson
  180. geoml >= "0.1.1"
  181. git
  182. git-cohttp
  183. git-cohttp-unix
  184. git-kv >= "0.2.0"
  185. git-mirage
  186. git-net
  187. git-split
  188. git-unix
  189. gitlab-unix
  190. glicko2
  191. gmap
  192. gobba
  193. gpt
  194. graphql
  195. graphql-async
  196. graphql-cohttp >= "0.13.0"
  197. graphql-lwt
  198. graphql_parser != "0.11.0"
  199. graphql_ppx
  200. h1
  201. h1_parser
  202. h2
  203. hacl
  204. hacl-star >= "0.6.0" & < "0.7.2"
  205. hacl_func
  206. hacl_x25519
  207. handlebars-ml >= "0.2.1"
  208. highlexer
  209. hkdf
  210. hockmd
  211. html_of_jsx
  212. http < "6.0.0"
  213. http-multipart-formdata < "2.0.0"
  214. httpaf >= "0.2.0"
  215. httpun
  216. httpun-ws
  217. hugin
  218. huml
  219. hvsock
  220. icalendar
  221. imagelib
  222. index
  223. inferno >= "20220603"
  224. influxdb-async
  225. influxdb-lwt
  226. inquire < "0.2.0"
  227. intel_hex >= "0.3"
  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" & < "1.1.1"
  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. layoutz
  262. letters
  263. liquid_ml >= "0.1.3"
  264. lmdb >= "1.0"
  265. logical
  266. logtk >= "1.6"
  267. lp
  268. lp-glpk
  269. lp-glpk-js < "0.5.0"
  270. lp-gurobi < "0.5.0"
  271. lru
  272. lt-code
  273. luv
  274. mbr-format
  275. mdx >= "1.6.0"
  276. mec
  277. mechaml >= "1.2.1"
  278. merlin = "4.17.1-501"
  279. merlin-lib >= "4.17.1-501"
  280. metrics
  281. middleware
  282. mimic
  283. minicaml = "0.3.1" | >= "0.4"
  284. mirage >= "4.0.0"
  285. mirage-block-partition
  286. mirage-block-ramdisk
  287. mirage-channel >= "4.0.1"
  288. mirage-crypto-ec
  289. mirage-flow-unix
  290. mirage-kv >= "2.0.0"
  291. mirage-kv-mem
  292. mirage-kv-unix >= "3.0.0"
  293. mirage-logs
  294. mirage-nat
  295. mirage-net-unix
  296. mirage-runtime < "4.7.0"
  297. mirage-tc
  298. mjson
  299. mlgpx
  300. mmdb < "0.3.0"
  301. mnd
  302. mqtt
  303. mrmime >= "0.2.0"
  304. msgpck >= "1.6"
  305. mssql >= "2.0.3"
  306. multibase
  307. multihash
  308. multihash-digestif
  309. multipart-form-data
  310. multipart_form
  311. multipart_form-eio
  312. multipart_form-lwt
  313. multipart_form-miou
  314. named-pipe
  315. nanoid
  316. nbd >= "4.0.3"
  317. nbd-tool
  318. neo4j_bolt
  319. nloge
  320. nocoiner
  321. non_empty_list
  322. nx
  323. nx-datasets
  324. nx-text
  325. OCADml >= "0.6.0"
  326. obatcher
  327. ocaml-index < "5.4.1-503"
  328. ocaml-r >= "0.4.0"
  329. ocaml-version >= "3.5.0"
  330. ocamlformat >= "0.13.0" & < "0.25.1"
  331. ocamlformat-lib
  332. ocamlformat-mlx-lib
  333. ocamlformat-rpc < "removed"
  334. ocamline
  335. ocluster < "0.3.0"
  336. ocue
  337. odoc < "2.1.1"
  338. oenv >= "0.1.0"
  339. ohex
  340. oidc
  341. opam-0install
  342. opam-0install-cudf >= "0.5.0"
  343. opam-compiler
  344. opam-file-format >= "2.1.1"
  345. opam-repomin
  346. opencage
  347. opentelemetry >= "0.6"
  348. opentelemetry-client-cohttp-eio
  349. opentelemetry-client-cohttp-lwt >= "0.6"
  350. opentelemetry-client-ocurl >= "0.6"
  351. opentelemetry-cohttp-lwt >= "0.6"
  352. opentelemetry-logs
  353. opentelemetry-lwt >= "0.6"
  354. opium
  355. opium-graphql
  356. opium-testing
  357. opium_kernel
  358. orewa
  359. orgeat
  360. ortac-core
  361. ortac-wrapper
  362. osnap < "0.3.0"
  363. osx-acl
  364. osx-attr
  365. osx-cf
  366. osx-fsevents
  367. osx-membership
  368. osx-mount
  369. osx-xattr
  370. otoggl
  371. owl >= "0.7.0" & != "0.9.0" & != "1.0.0"
  372. owl-base < "0.5.0"
  373. owl-ode >= "0.1.0" & != "0.2.0"
  374. owl-symbolic
  375. passe
  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. public-suffix
  429. pyast
  430. qcaml
  431. qcheck >= "0.25"
  432. qcheck-alcotest
  433. qcheck-core >= "0.25"
  434. qcow-stream >= "0.13.0"
  435. qcow-tool >= "0.13.0"
  436. qcow-types >= "0.13.0"
  437. query-json
  438. quickjs
  439. quill
  440. randii
  441. reason-standard
  442. red-black-tree
  443. reparse >= "2.0.0" & < "3.0.0"
  444. reparse-unix < "2.1.0"
  445. resp
  446. resp-unix >= "0.10.0"
  447. resto >= "0.8"
  448. rfc1951 < "1.0.0"
  449. routes < "2.0.0"
  450. rpc
  451. rpclib
  452. rpclib-async
  453. rpclib-lwt
  454. rpmfile < "0.3.0"
  455. rpmfile-eio
  456. rpmfile-unix
  457. rune
  458. SZXX >= "4.0.0"
  459. saga
  460. salsa20
  461. salsa20-core
  462. sanddb >= "0.2"
  463. scrypt-kdf
  464. secp256k1 >= "0.4.1"
  465. secp256k1-internal
  466. semver >= "0.2.1"
  467. sendmail
  468. sendmail-lwt
  469. sendmail-miou-unix
  470. sendmail-mirage
  471. sendmsg
  472. seqes
  473. server-reason-react
  474. session-cookie
  475. session-cookie-async
  476. session-cookie-lwt
  477. shakuhachi
  478. sherlodoc
  479. sihl < "0.2.0"
  480. sihl-type
  481. slug
  482. smaws-clients
  483. smaws-lib
  484. smol
  485. smol-helpers
  486. sodium-fmt
  487. solidity-alcotest
  488. sowilo
  489. spdx_licenses
  490. spectrum >= "0.2.0"
  491. spectrum_capabilities
  492. spectrum_palette_ppx
  493. spectrum_palettes
  494. spectrum_tools
  495. spin >= "0.7.0"
  496. spurs < "0.1.1"
  497. squirrel
  498. ssh-agent
  499. ssl >= "0.6.0"
  500. stramon-lib
  501. stringx
  502. styled-ppx
  503. swapfs
  504. symex >= "0.2"
  505. synchronizer >= "0.2"
  506. syslog-rfc5424 < "0.2"
  507. talon
  508. tcpip
  509. tdigest < "2.1.0"
  510. term-indexing
  511. term-tools
  512. terminal
  513. terminal_size >= "0.1.1"
  514. terminus
  515. terminus-cohttp
  516. terminus-hlc
  517. terml
  518. testo
  519. testo-lwt
  520. textmate-language >= "0.3.0"
  521. textrazor
  522. timedesc
  523. timere
  524. timmy
  525. timmy-jsoo
  526. timmy-lwt
  527. timmy-unix
  528. tls >= "0.12.8"
  529. toc
  530. topojson
  531. topojsone
  532. traits
  533. transept
  534. tsort >= "2.2.0"
  535. twostep
  536. type_eq
  537. type_id
  538. typeid >= "1.0.1"
  539. tyre >= "0.4"
  540. tyxml >= "4.2.0"
  541. tyxml-jsx
  542. tyxml-ppx >= "4.3.0"
  543. tyxml-syntax
  544. uecc
  545. ulid
  546. universal-portal
  547. unix-dirent
  548. unix-errno
  549. unix-sys-resource
  550. unix-sys-stat
  551. unix-time
  552. unstrctrd
  553. uring < "0.4"
  554. user-agent-parser
  555. uspf
  556. uspf-lwt
  557. uspf-mirage
  558. uspf-unix
  559. utcp
  560. utop >= "2.13.0"
  561. validate
  562. validator
  563. vercel
  564. vhd-format-lwt >= "0.13.0"
  565. wayland >= "2.0"
  566. wcwidth
  567. websocketaf
  568. x509 >= "0.7.0"
  569. xapi-rrd
  570. xapi-stdext-date
  571. xapi-stdext-encodings
  572. xapi-stdext-std >= "4.16.0"
  573. yaml
  574. yaml-sexp
  575. yocaml
  576. yocaml_syndication >= "2.0.0"
  577. yocaml_yaml < "2.0.0"
  578. yojson >= "1.6.0"
  579. yojson-five
  580. yuscii >= "0.3.0"
  581. yuujinchou >= "1.0.0"
  582. zar
  583. zed >= "3.2.2"
  584. zlist < "0.4.0"

Conflicts (1)

  1. result < "1.5"