OCaml By Examples

test coverage

lib.ml

Test coverage allows us to understand how much is covered by our inline tests.
let great = function 
  | 0 -> "zero"
  | 1 | 2 -> "1 or 2"
  | 3 -> "three"
  | _ -> "the rest"

let%test "some test" = (great 0 = "zero")

let%test "some other test" = (great 1 = "1 or 2")

dune

(library
 (name lib)
 (libraries)
 (inline_tests)
 (preprocess
  (pps ppx_inline_test))
To use it, simply add the [bisect_ppx](https://github.com/aantron/bisect_ppx) library as an *instrumentation backend*.
 (instrumentation
  (backend bisect_ppx)))

console

Make sure to install the library
$ opam install bisect_ppx
Pass this command to generate the coverage data based on your tests.
$ find . -name '*.coverage' | xargs rm -f
$ dune runtest --instrument-with bisect_ppx --force
Via `bisect-ppx-report` you can generate a report within your terminal
$ bisect-ppx-report summary
Coverage: 8/11 (72.73%)
Or as an HTML report (prettier).
$ bisect-ppx-report html
Info: found coverage files in './_build/default/'
next: publishing