Testing OCaml Code with Alcotest

Every example in this book ships with tests, and they all use the same library: Alcotest, a small, clear test framework. This chapter shows the pattern with the example in source-code/testing/: a tiny library module and a test suite over it. Once you see the shape here, every other test directory in the book will read the same way.

Project Layout: Library, Program, and Tests

A dune project splits into three parts, each in its own directory with its own dune file:

1 testing/
2 ├── src/my_colors.ml    <- the library implementation
3 ├── src/my_colors.mli   <- the library's public interface
4 ├── bin/main.ml         <- a program that uses the library
5 └── test/               <- the Alcotest suite

The src/dune file declares a library:

1 (library
2  (name my_colors))

The test/dune file declares a test that depends on that library and on Alcotest:

1 (test
2  (name test_my_colors)
3  (libraries my_colors alcotest))

Keeping the real code in a library, separate from the program and the tests, is worth the small extra structure. Both the program and the test suite link against the same library, so you test the exact code you ship.

The Interface File

The file src/my_colors.mli is an interface. It lists the types and functions the module makes public. Anything not listed stays private, which keeps a module’s surface small and its internals free to change:

1 type t = Orange | Red | Blue | Green | Silver
2 
3 val show : t -> string
4 
5 (* Alphabetical by constructor name, like the Haskell custom Ord instance. *)
6 val compare : t -> t -> int
7 val equal : t -> t -> bool
8 val ( < ) : t -> t -> bool
9 val ( > ) : t -> t -> bool

Each val line gives a name and its type. Note the last two: this module exports its own < and > operators, so callers can compare colors with the same symbols they use on numbers.

The Implementation

The file src/my_colors.ml provides a body for every val in the interface. The color type orders its values by name text rather than by declaration order:

 1 type t = Orange | Red | Blue | Green | Silver
 2 
 3 let show = function
 4   | Orange -> "Orange"
 5   | Red -> "Red"
 6   | Blue -> "Blue"
 7   | Green -> "Green"
 8   | Silver -> "Silver"
 9 
10 (* Custom order: alphabetical by constructor name. *)
11 let compare a b = String.compare (show a) (show b)
12 
13 let equal a b = compare a b = 0
14 let ( < ) a b = compare a b < 0
15 let ( > ) a b = compare a b > 0

Because compare sorts by the show text, the order is alphabetical: Blue, Green, Orange, Red, Silver. The < and > operators are defined on top of compare, so they follow the same rule. The program in bin/main.ml shows this with a local open:

1 let () = Printf.printf "%b\n" My_colors.(Red < Green)

The form My_colors.(...) opens the module just for the expression in the parentheses, so < there means My_colors.( < ). With alphabetical order, “Green” sorts before “Red”, so Red < Green is false, and the program prints false.

Writing Tests

The file test/test_my_colors.ml holds the suite. A test is a function of type unit -> unit that makes one or more checks. The core check is Alcotest.check, which takes a testable (a description of how to compare and print a type), a message, the expected value, and the actual value:

1 let test_head () =
2   Alcotest.(check int) "first Int" 1 (List.hd [ 1; 2; 3; 4 ]);
3   Alcotest.(check string) "first string" "the" (List.hd [ "the"; "dog"; "ran" ])

Alcotest.(check int) opens the Alcotest module so int names the built-in testable for integers; check string and check bool work the same way. If the expected and actual values differ, Alcotest prints both and fails the case.

To test that code raises an exception, use check_raises. It runs a function and checks that it throws the exact exception you name:

1 let test_head_empty () =
2   Alcotest.check_raises "empty list raises"
3     (Failure "hd") (fun () -> ignore (List.hd ([] : int list)))

List.hd on an empty list raises Failure "hd", so this case passes. The type annotation ([] : int list) tells the compiler which empty list this is, since [] alone is polymorphic.

The color tests check the custom order:

1 let test_descending () =
2   Alcotest.(check bool) "Green < Red" true My_colors.(Green < Red)
3 
4 let test_ascending () =
5   Alcotest.(check bool) "Red > Silver is false" false My_colors.(Red > Silver)

“Green” sorts before “Red”, so Green < Red is true. “Red” does not sort after “Silver”, so Red > Silver is false. Both checks confirm the alphabetical rule.

Running the Suite

The main function names the suite and groups the cases. Alcotest.run takes a suite name and a list of groups; each group is a name and a list of test_case values:

1 let () =
2   Alcotest.run "MyColors"
3     [ ( "head",
4         [ Alcotest.test_case "returns the first element" `Quick test_head;
5           Alcotest.test_case "throws on empty list" `Quick test_head_empty ] );
6       ( "MyColors",
7         [ Alcotest.test_case "custom compare descending" `Quick test_descending;
8           Alcotest.test_case "custom compare ascending" `Quick test_ascending ] );
9     ]

test_case takes a label, a speed level (`Quick or `Slow so you can skip slow tests when you want a fast run), and the test function. Run the whole thing with:

1 cd source-code/testing
2 dune runtest

Alcotest prints one line per case and a summary. On a failure it shows the group, the case, and the expected against the actual value, so you can jump straight to the problem. Every example in this book responds to dune runtest, and now you can read any of those suites: they all use the check, check_raises, test_case, and run pieces you just saw.