Not able to access library code in tests in a Dune project

Hi folks,

Hopefully this is the right place for this question. I have a Dune project:

$ tree
.
├── bin
│   ├── dune
│   └── main.ml
├── compiler.opam
├── dune-project
├── lib
│   ├── dune
│   └── foo.ml
└── test
    ├── compiler.ml
    └── dune

where test/compiler.ml contains the following:

open OUnit2

let tests =  
  "foo" >:::
  ["returns the value passed" >:: 
   (fun _ -> assert_equal 2  (foo 2))]

 let _ = run_test_tt_main tests

The function foo is defined in lib/foo.ml as

let foo x = x

When I try to build with dune build, I get:

$ dune build
File "test/compiler.ml", line 6, characters 30-33:
6 |    (fun _ -> assert_equal 2  (foo 2))]
                                  ^^^
Error: Unbound value foo

How do I get the tests to see the code in my library? My lib/dune contains

(library
 (name compiler))

and test/dune has

(test
 (name compiler)
 (libraries ounit2 compiler))

Thanks!

foo (the function) is not in scope. You can refer to it as Compiler.Foo.foo in test/compiler.ml (Compiler because it’s the name of the library, Foo because of foo.ml that defines it)

5 Likes

Thank you, that works!

I’m using VS Code and even after I’ve done a dune build, the editor still doesn’t find it (Unbound module Compiler). Do you happen to know how to solve that?

Try using a different name for your library and your test, for example test/test_compiler.ml.

3 Likes

Sorted, thanks again! I’m new to OCaml so am getting used to how to do things.