Ocaml Unit Testing

Basic workflow for testing

  • Write a function in a file f.ml.
  • Write unit tests for that function in a separate file test.ml.
  • Build and run test to execute the unit tests.

Example

sum.ml

1
2
3
let rec sum = function
  | [] -> 0
  | x :: xs -> x + sum xs

test.ml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
open OUnit2
open Sum

let tests = "test suite for sum" >::: [
    (*assert_equal checks whether its two arguments are equal*)
  "empty" >:: (fun _ -> assert_equal 0 (sum []));
  "singleton" >:: (fun _ -> assert_equal 1 (sum [1]));
  "two_elements" >:: (fun _ -> assert_equal 3 (sum [1; 2]));
]

let _ = run_test_tt_main tests

To improve output, we have

1
2
3
4
5
let tests = "test suite for sum" >::: [
  "empty" >:: (fun _ -> assert_equal 0 (sum []) ~printer:string_of_int);
  "singleton" >:: (fun _ -> assert_equal 1 (sum [1]) ~printer:string_of_int);
  "two_elements" >:: (fun _ -> assert_equal 3 (sum [1; 2]) ~printer:string_of_int);
]

To improve the code, we have

1
2
3
4
5
6
7
8
let make_sum_test name expected_output input =
  name >:: (fun _ -> assert_equal expected_output (sum input) ~printer:string_of_int)

let tests = "test suite for sum" >::: [
  make_sum_test "empty" 0 [];
  make_sum_test "singleton" 1 [1];
  make_sum_test "two_elements" 3 [1; 2];
]
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy