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. alcobar
  4. alcotest-async < "1.7.0"
  5. alg_structs_qcheck
  6. algaeff
  7. ambient-context
  8. ambient-context-eio
  9. ambient-context-lwt
  10. angstrom >= "0.7.0"
  11. ansi >= "0.6.0"
  12. anycache >= "0.7.4"
  13. anycache-async
  14. anycache-lwt
  15. arc
  16. archetype >= "1.4.2"
  17. archi
  18. arp
  19. arrakis < "1.1.0"
  20. art < "0.3.0"
  21. asai
  22. asak >= "0.2"
  23. asli >= "0.2.0"
  24. asn1-combinators >= "0.2.5"
  25. atd >= "2.3.3"
  26. atdgen >= "2.10.0"
  27. atdpy
  28. atdts
  29. base32
  30. base64 >= "2.1.2" & < "3.2.0" | >= "3.4.0"
  31. bastet
  32. bastet_async
  33. bastet_lwt
  34. bech32
  35. bechamel >= "0.5.0"
  36. bigarray-overlap
  37. bigstringaf
  38. biotk >= "0.4"
  39. bitlib
  40. blake2
  41. bloomf
  42. bls12-381 < "0.4.1" | >= "3.0.0" & < "18.0"
  43. bls12-381-hash
  44. bls12-381-js >= "0.4.2"
  45. bls12-381-js-gen >= "0.4.2"
  46. bls12-381-legacy
  47. bls12-381-signature
  48. bls12-381-unix
  49. blurhash
  50. brisk-reconciler
  51. builder-web
  52. bytebuffer
  53. ca-certs
  54. ca-certs-nss
  55. cactus
  56. caldav
  57. calendar >= "3.0.0"
  58. calendars >= "2.0.0"
  59. callipyge
  60. camlix
  61. camlkit
  62. camlkit-base
  63. capnp-rpc < "1.2.3"
  64. capnp-rpc-unix < "1.2.3"
  65. caqti >= "1.7.0"
  66. caqti-async >= "1.7.0"
  67. caqti-driver-mariadb >= "1.7.0"
  68. caqti-driver-postgresql >= "1.7.0"
  69. caqti-driver-sqlite3 >= "1.7.0"
  70. caqti-dynload >= "2.0.1"
  71. caqti-eio
  72. caqti-lwt >= "1.7.0"
  73. caqti-miou
  74. carray
  75. carton < "1.0.0"
  76. carton-git
  77. carton-lwt >= "0.4.3" & < "1.0.0"
  78. catala >= "0.6.0"
  79. cborl
  80. cf-lwt
  81. chacha
  82. chamelon
  83. chamelon-unix
  84. charrua-client
  85. charrua-server
  86. checkseum >= "0.0.3"
  87. cid
  88. clarity-lang
  89. class_group_vdf
  90. cohttp < "6.0.0"
  91. cohttp-curl-async < "6.1.0"
  92. cohttp-eio = "6.0.0~beta2"
  93. colombe >= "0.2.0"
  94. color
  95. commons
  96. conan
  97. conan-cli
  98. conan-database
  99. conan-lwt
  100. conan-unix
  101. conex < "0.10.0"
  102. conex-mirage-crypto
  103. conformist
  104. cookie
  105. cow >= "2.2.0"
  106. crockford
  107. css
  108. css-parser
  109. cstruct
  110. cstruct-sexp
  111. ctypes-zarith
  112. cuid
  113. curly
  114. current
  115. current-albatross-deployer
  116. current_git >= "0.7.1"
  117. current_incr
  118. current_rpc >= "0.7.4"
  119. data-encoding
  120. dates_calc
  121. dbase4
  122. decimal >= "0.3.0"
  123. decompress < "1.5.3"
  124. depyt
  125. digestif >= "0.9.0"
  126. dispatch >= "0.4.1"
  127. dkim
  128. dkim-bin
  129. dkim-mirage
  130. dkml-dune-dsl-show
  131. dkml-install
  132. dkml-install-installer
  133. dkml-install-runner
  134. dmarc
  135. dns >= "4.4.1"
  136. dns-cli
  137. dns-client >= "4.6.3"
  138. dns-forward-lwt-unix
  139. dns-resolver
  140. dns-server
  141. dns-tsig
  142. dnssd
  143. dnssec < "10.2.4"
  144. domain-name
  145. dream
  146. dream-pure
  147. duff
  148. dune-deps >= "1.4.0"
  149. dune-release >= "1.0.0"
  150. duration
  151. echo
  152. eio < "0.12"
  153. eio_linux < "0.12"
  154. eio_windows < "0.12"
  155. emile
  156. encore
  157. eqaf >= "0.5"
  158. equinoxe
  159. equinoxe-cohttp
  160. equinoxe-hlc
  161. ezgzip
  162. ezjsonm
  163. ezjsonm-lwt
  164. FPauth
  165. FPauth-core
  166. FPauth-responses
  167. FPauth-strategies
  168. faraday != "0.2.0"
  169. farfadet
  170. fat-filesystem
  171. fehu < "1.0.0~alpha3"
  172. ff
  173. ff-pbt
  174. flex-array
  175. flux
  176. fluxt
  177. forester >= "5.0"
  178. fsevents-lwt
  179. functoria
  180. fungi
  181. geojson
  182. geoml >= "0.1.1"
  183. git
  184. git-cohttp
  185. git-cohttp-unix
  186. git-kv >= "0.2.0"
  187. git-mirage
  188. git-net
  189. git-split
  190. git-unix
  191. gitlab-unix
  192. glicko2
  193. gmap
  194. gobba
  195. gpt
  196. graphql
  197. graphql-async
  198. graphql-cohttp >= "0.13.0"
  199. graphql-lwt
  200. graphql_parser != "0.11.0"
  201. graphql_ppx
  202. h1
  203. h1_parser
  204. h2
  205. hacl
  206. hacl-star >= "0.6.0" & < "0.7.2"
  207. hacl_func
  208. hacl_x25519
  209. handlebars-ml >= "0.2.1"
  210. highlexer
  211. hkdf
  212. hockmd
  213. html_of_jsx
  214. http < "6.0.0"
  215. http-multipart-formdata < "2.0.0"
  216. httpaf >= "0.2.0"
  217. httpun
  218. httpun-ws
  219. hugin < "1.0.0~alpha3"
  220. huml
  221. hvsock
  222. icalendar
  223. imagelib
  224. index
  225. inferno >= "20220603"
  226. influxdb-async
  227. influxdb-lwt
  228. inquire < "0.2.0"
  229. intel_hex >= "0.3"
  230. interval-map
  231. iomux
  232. irmin
  233. irmin-bench
  234. irmin-chunk
  235. irmin-cli
  236. irmin-containers
  237. irmin-fs
  238. irmin-git
  239. irmin-graphql
  240. irmin-pack
  241. irmin-pack-tools
  242. irmin-test < "3.6.1"
  243. irmin-tezos
  244. irmin-unix
  245. irmin-watcher
  246. jekyll-format
  247. jose
  248. json-data-encoding >= "0.9" & < "1.1.1"
  249. json_decoder
  250. jsonfeed
  251. jsonxt
  252. junit_alcotest < "2.1.0"
  253. jwto
  254. kaun < "1.0.0~alpha3"
  255. kdf
  256. ke >= "0.2"
  257. kkmarkdown
  258. kmt
  259. lambda-runtime
  260. lambda_streams
  261. lambda_streams_async
  262. lambdapi
  263. layoutz
  264. letters
  265. liquid_ml >= "0.1.3"
  266. lmdb >= "1.0"
  267. logical
  268. logtk >= "1.6"
  269. lp
  270. lp-glpk
  271. lp-glpk-js < "0.5.0"
  272. lp-gurobi < "0.5.0"
  273. lru
  274. lt-code
  275. luv
  276. mbr-format
  277. mdx >= "1.6.0"
  278. mec
  279. mechaml >= "1.2.1"
  280. merlin = "4.17.1-501"
  281. merlin-lib >= "4.17.1-501"
  282. metrics
  283. middleware
  284. mimic
  285. minicaml = "0.3.1" | >= "0.4"
  286. mirage >= "4.0.0"
  287. mirage-block-partition
  288. mirage-block-ramdisk
  289. mirage-channel >= "4.0.1"
  290. mirage-crypto-ec
  291. mirage-flow-unix
  292. mirage-kv >= "2.0.0"
  293. mirage-kv-mem
  294. mirage-kv-unix >= "3.0.0"
  295. mirage-logs
  296. mirage-nat
  297. mirage-net-unix
  298. mirage-runtime < "4.7.0"
  299. mirage-tc
  300. mjson
  301. mlgpx
  302. mmdb < "0.3.0"
  303. mnd
  304. mqtt
  305. mrmime >= "0.2.0"
  306. msgpck >= "1.6"
  307. mssql >= "2.0.3"
  308. multibase
  309. multihash
  310. multihash-digestif
  311. multipart-form-data
  312. multipart_form
  313. multipart_form-eio
  314. multipart_form-lwt
  315. multipart_form-miou
  316. named-pipe
  317. nanoid
  318. nbd >= "4.0.3"
  319. nbd-tool
  320. neo4j_bolt
  321. nloge
  322. nocoiner
  323. non_empty_list
  324. nx < "1.0.0~alpha3"
  325. nx-datasets
  326. nx-text
  327. OCADml >= "0.6.0"
  328. obatcher
  329. ocaml-index < "5.4.1-503"
  330. ocaml-r >= "0.4.0"
  331. ocaml-version >= "3.5.0"
  332. ocamlformat >= "0.13.0" & < "0.25.1"
  333. ocamlformat-lib
  334. ocamlformat-mlx-lib
  335. ocamlformat-rpc < "removed"
  336. ocamline
  337. ocluster < "0.3.0"
  338. ocue
  339. odoc < "2.1.1"
  340. oenv >= "0.1.0"
  341. ohex
  342. oidc
  343. opam-0install
  344. opam-0install-cudf >= "0.5.0"
  345. opam-compiler
  346. opam-file-format >= "2.1.1"
  347. opam-repomin
  348. opencage
  349. opentelemetry >= "0.6"
  350. opentelemetry-client-cohttp-eio
  351. opentelemetry-client-cohttp-lwt >= "0.6"
  352. opentelemetry-client-ocurl >= "0.6"
  353. opentelemetry-cohttp-lwt >= "0.6"
  354. opentelemetry-logs
  355. opentelemetry-lwt >= "0.6"
  356. opium
  357. opium-graphql
  358. opium-testing
  359. opium_kernel
  360. orewa
  361. orgeat
  362. ortac-core
  363. ortac-wrapper
  364. osnap < "0.3.0"
  365. osx-acl
  366. osx-attr
  367. osx-cf
  368. osx-fsevents
  369. osx-membership
  370. osx-mount
  371. osx-xattr
  372. otoggl
  373. owl >= "0.7.0" & != "0.9.0" & != "1.0.0"
  374. owl-base < "0.5.0"
  375. owl-ode >= "0.1.0" & != "0.2.0"
  376. owl-symbolic
  377. parseff
  378. passe
  379. passmaker
  380. patch < "3.0.0~alpha2"
  381. pbkdf
  382. pecu >= "0.2"
  383. pf-qubes
  384. pg_query >= "0.9.6"
  385. pgx >= "1.0"
  386. pgx_unix >= "1.0"
  387. pgx_value_core
  388. pgx_value_ptime
  389. phylogenetics
  390. piaf
  391. plebeia >= "2.0.0"
  392. polyglot
  393. polynomial
  394. ppx_blob >= "0.3.0"
  395. ppx_deriving_cmdliner
  396. ppx_deriving_ezjsonm
  397. ppx_deriving_qcheck
  398. ppx_deriving_rpc
  399. ppx_deriving_yaml
  400. ppx_inline_alcotest
  401. ppx_marshal
  402. ppx_parser
  403. ppx_protocol_conv >= "5.0.0"
  404. ppx_protocol_conv_json >= "5.0.0"
  405. ppx_protocol_conv_jsonm >= "5.0.0"
  406. ppx_protocol_conv_msgpack >= "5.0.0"
  407. ppx_protocol_conv_xml_light >= "5.0.0"
  408. ppx_protocol_conv_xmlm
  409. ppx_protocol_conv_yaml >= "5.0.0"
  410. ppx_repr
  411. ppx_subliner
  412. ppx_units
  413. ppx_yojson >= "1.1.0"
  414. pratter
  415. prbnmcn-ucb1 >= "0.0.2"
  416. prc
  417. preface
  418. pretty_expressive
  419. prettym
  420. proc-smaps
  421. producer < "0.2.0"
  422. progress
  423. prom
  424. prometheus < "1.2"
  425. prometheus-app
  426. protocell
  427. protocol-9p < "0.11.0" | >= "0.11.2"
  428. protocol-9p-unix
  429. proton
  430. psq
  431. public-suffix
  432. pyast
  433. qcaml
  434. qcheck >= "0.25"
  435. qcheck-alcotest
  436. qcheck-core >= "0.25"
  437. qcow-stream >= "0.13.0"
  438. qcow-tool >= "0.13.0"
  439. qcow-types >= "0.13.0"
  440. query-json
  441. quickjs
  442. quill < "1.0.0~alpha3"
  443. randii
  444. reason-standard
  445. red-black-tree
  446. reparse >= "2.0.0" & < "3.0.0"
  447. reparse-unix < "2.1.0"
  448. resp
  449. resp-unix >= "0.10.0"
  450. resto >= "0.8"
  451. rfc1951 < "1.0.0"
  452. routes < "2.0.0"
  453. rpc
  454. rpclib
  455. rpclib-async
  456. rpclib-lwt
  457. rpmfile < "0.3.0"
  458. rpmfile-eio
  459. rpmfile-unix
  460. rune < "1.0.0~alpha3"
  461. SZXX >= "4.0.0"
  462. saga
  463. salsa20
  464. salsa20-core
  465. sanddb >= "0.2"
  466. scrypt-kdf
  467. secp256k1 >= "0.4.1"
  468. secp256k1-internal
  469. semver >= "0.2.1"
  470. sendmail
  471. sendmail-lwt
  472. sendmail-miou-unix
  473. sendmail-mirage
  474. sendmsg
  475. seqes
  476. server-reason-react
  477. session-cookie
  478. session-cookie-async
  479. session-cookie-lwt
  480. shakuhachi
  481. sherlodoc
  482. sihl < "0.2.0"
  483. sihl-type
  484. slug
  485. smaws-clients
  486. smaws-lib
  487. smol
  488. smol-helpers
  489. sodium-fmt
  490. solidity-alcotest
  491. sowilo < "1.0.0~alpha3"
  492. spdx_licenses
  493. spectrum >= "0.2.0"
  494. spectrum_capabilities
  495. spectrum_palette_ppx
  496. spectrum_palettes
  497. spectrum_tools
  498. spin >= "0.7.0"
  499. spurs < "0.1.1"
  500. squirrel
  501. ssh-agent
  502. ssl >= "0.6.0"
  503. stramon-lib
  504. stringx
  505. styled-ppx
  506. swapfs
  507. symex >= "0.2"
  508. synchronizer >= "0.2"
  509. syslog-rfc5424 < "0.2"
  510. talon < "1.0.0~alpha3"
  511. tcpip
  512. tdigest < "2.1.0"
  513. term-indexing
  514. term-tools
  515. terminal
  516. terminal_size >= "0.1.1"
  517. terminus
  518. terminus-cohttp
  519. terminus-hlc
  520. terml
  521. testo
  522. testo-lwt
  523. textmate-language >= "0.3.0"
  524. textrazor
  525. timedesc
  526. timere
  527. timmy
  528. timmy-jsoo
  529. timmy-lwt
  530. timmy-unix
  531. tls >= "0.12.8"
  532. toc
  533. topojson
  534. topojsone
  535. traits
  536. transept
  537. tsort >= "2.2.0"
  538. twostep
  539. type_eq
  540. type_id
  541. typeid >= "1.0.1"
  542. tyre >= "0.4"
  543. tyxml >= "4.2.0"
  544. tyxml-jsx
  545. tyxml-ppx >= "4.3.0"
  546. tyxml-syntax
  547. uecc
  548. ulid
  549. universal-portal
  550. unix-dirent
  551. unix-errno
  552. unix-sys-resource
  553. unix-sys-stat
  554. unix-time
  555. unstrctrd
  556. uring < "0.4"
  557. user-agent-parser
  558. uspf
  559. uspf-lwt
  560. uspf-mirage
  561. uspf-unix
  562. utcp
  563. utop >= "2.13.0"
  564. validate
  565. validator
  566. vercel
  567. vhd-format-lwt >= "0.13.0"
  568. wayland >= "2.0"
  569. wcwidth
  570. websocketaf
  571. x509 >= "0.7.0"
  572. xapi-rrd
  573. xapi-stdext-date
  574. xapi-stdext-encodings
  575. xapi-stdext-std >= "4.16.0"
  576. yaml
  577. yaml-sexp
  578. yocaml
  579. yocaml_syndication >= "2.0.0"
  580. yocaml_yaml < "2.0.0"
  581. yojson >= "1.6.0"
  582. yojson-five
  583. yuscii >= "0.3.0"
  584. yuujinchou >= "1.0.0"
  585. zar
  586. zed >= "3.2.2"
  587. zlist < "0.4.0"

Conflicts (1)

  1. result < "1.5"