Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe: OUnit assertions #472

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions content/languages/ocaml/recipes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: OCaml recipes for Codewars
tags: [authoring, recipe, ocaml]
---


## 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:

```ocaml
"(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"
)
```

```text
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

- [Improving OUnit Output](https://cs3110.github.io/textbook/chapters/data/ounit.html#improving-ounit-output)
- [OUnit: xUnit testing framework for OCaml](https://ocaml.org/p/ounit2/2.2.3/doc/index.html#error-reporting)
5 changes: 4 additions & 1 deletion sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,10 @@ module.exports = {
type: "doc",
id: "languages/ocaml/index",
},
items: ["languages/ocaml/ounit"],
items: [
"languages/ocaml/ounit",
"languages/ocaml/recipes"
],
},
{
type: "category",
Expand Down