ocamlocaml-dune

Library ounit2 not found


I want to run unit test with OCaml and OUnit2 I have followed the instructions in this tutorial https://cs3110.github.io/textbook/chapters/data/ounit.html

However, when I run

dune build

I get the error

$ dune build
File "test/dune", line 3, characters 12-18:
3 |  (libraries ounit2))
                ^^^^^^
Error: Library "ounit2" not found.
-> required by _build/default/test/test.exe
-> required by alias test/all
-> required by alias default

My directory structure looks like this:

│   dune-project
│   mon_project.opam
│
├───bin
│       dune
│       main.ml
│       sum.ml
│
├───lib
│       dune
│
├───test
│       dune
│       mon_project.ml
│       test.ml

The dune file in the directory test looks like this:

(executable
 (name test)
 (libraries ounit2))

sum.ml and test.ml are exactly like in the tutorial.

What do I do wrong?


Solution

  • As an educated guess based on reading the instructions you linked to which don't mention installation of libraries at all, you have not installed the ounit2 library via opam. Dune is a build management system, but not a package management system and will not install necessary dependencies for you.

    Fortunately, it's easy to install the needed library.

    opam install ounit2
    

    Once installed you'll be able to find out where it's installed with:

    ocamlfind query ounit2