Running OUnit tests using dune
Asked Answered
A

1

6

I'm having difficulties running OUnit tests, mostly because I'm new to both dune and OUnit. dune complains when I run dune runtest:

File "test/dune", line 4, characters 13-14:
Error: Library "f" not found.
Hint: try: dune external-lib-deps --missing @runtest

Here's the project structure:

├── dune
├── f.ml  # This is the source file.
└── test
    ├── dune
    └── f_test.ml  # This is the test.

This is dune:

(executable
  (name f))

This is test/dune:

(test
  (name f_test)
  (libraries oUnit f))  ; <- `f` here causes problems.

I can see that the error appears because dune does not know about f.ml, and hence does not know about f in the dune file.

How can I make dune compile f.ml in such a way that test/dune knows about the f library that I use in test/f_test.ml? How can I run the unit tests properly?

Armillary answered 15/11, 2018 at 10:34 Comment(0)
R
8

One possibility is to split f into a private library and an executable, and then test the split library.

EDIT:

For instance, the project structure could be updated to

├── dune
├── f.ml  # f only contains the I/O glue code.
├── lib
|    ├── dune
|    └── a.ml  # a implements the features that need to be tested.
└── test
    ├── dune
    └── test.ml  # This is the test.

with dune

 (executable (name main) (libraries Lib)) 

For the test, test/dune:

(test (name test) (libraries Lib oUnit))

and finally lib/dune

(library (name Lib))

With this setup, the test can be run with dune runtest.

Resume answered 15/11, 2018 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.