Skip to main content

OCaml recipes for Codewars

Informative assertion messages

Default assertion messages of OCaml OUnit are usually very bad. OUnit assertions accept two optional arguments:

  • ~printer defines a printer (stringifier) for compared values.
  • ~msg is used to provide additional information about failure, if necessary.

With both arguments used, test output becomes more explicit:

"(square 3) should return 9" >:: (fun _ ->
  let actual = square 3 in
  assert_equal 9 actual ~printer: string_of_int ~msg: "Incorrect answer for n=3"
)
"(square 3) should return 9" >:: (fun _ ->
  let actual = square 3 in
  assert_equal 9 actual ~printer: string_of_int ~msg: "Incorrect answer for n=3"
)
Tests for square
    (square 3) should return 9
        Incorrect answer for n=3
        expected: 9 but got: 10
    Completed in 0.22ms
Completed in 0.24ms
Tests for square
    (square 3) should return 9
        Incorrect answer for n=3
        expected: 9 but got: 10
    Completed in 0.22ms
Completed in 0.24ms

References