2

I have two libraries:

 lib/ dune src.ml src.mli test/ dune test.ml 

both are libraries and I am testing in the test library with ppx_inline_test as i do not want them to be included in the library

dune file in test looks like this:

(library (name Test_my_lib) (preprocess (pps ppx_inline_test ppx_expect)) (libraries base my_other_lib pp ppx_inline_test) (inline_tests) ) 

other one is identical without the inline tests

Obviously I can't access the functions from src.ml in the test library that way.

Is there a way I can test these functions from a different module without making them public? If not how can I easily test functions from another module without having the tests backed into the library (I am avoiding that mainly because I thought it's good practice)?

I've tried putting a separate .mli file including more definitions in the test module but that way it complains about not finding the source.

1
  • 1
    Not a full answer but the test project you have can be useful to test the library only from its public interface. A lot of people say that you should only write unit tests using its public API (see Things I’ve learned from writing a lot of unit tests which quotes notably Kent Beck from the Agile Manifesto). Commented Sep 5, 2024 at 15:27

1 Answer 1

2

There's nothing special about the test library from the compilers perspective, so anything the testing library needs must be in the public interface of the module it is testing.

If you do need something to test the module that doesn't really belong in the public interface, there are two options I usually see:

  1. Write the tests inline in the module instead of in a separate library. As you note, this is typically avoided, but in some cases it is preferable.
  2. Expose the values you need for testing in the public interface, but in a way that makes it clear they're not really for public use. For example, you could put those values in a module named For_testing or Private.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.