Using files in tests with Dune

Hi folks,

Often, when I write tests, I want to use separate files for, say, expected output rather than putting that in the .ml file of the tests.

If I just create a file under test (i.e next to the test module itself) and try to read that in the test:

open OUnit2

let tests =
  "it does something" >::
    (fun _ ->
      let  _ = 
      open_in "some_file"
      |> input_line in


let _ = run_test_tt_main tests

then the test will fail with a Sys_error("some_file: No such file or directory") because it’s being run from the _build/default/test directory and the file hasn’t been copied there.

How do I copy the file to the build directory with Dune so that it can be found when running the test?

I tried using a copy action in my dune file in the test directory:

 (name my_project)
 (libraries ounit2)
 (action (copy %{project_root}/test/some_file .)))

but that fails with

Error: Aliases must not have targets, however I inferred that these files
will be created by this action:
- test

What is the correct way to do this?

Thank you.

You want to add the files as deps (see the rule stanza docs. Pathing is relative from the dune file by the way, and it seems like they are in the same directory.

I can confirm that this works. You can look at my project here: