Tests of private fields / functions

So as I'm really an amateur in this I was wondering what do you guys do in terms of folder structure to add tests for internals of your crates, which are not exposed to the user? Do you make a separate test module in the main library, do you just add the tests in the same module/file as the data structure? What is seen as the best design solution.

The book has information about this: Testing

It's not super specifically spelled out, but each each module that has tests has its own associated tests module, rather than one big one.

Do you guys then do any outside tests? or just each module has a mirroring test (or maybe submodule). I used to have a tests folder with mirroring modules for test, but I guess this might be better.

External tests use Testing

I personally won't add unit tests unless there's some complex logic happening that's tricky to get right. In that case, the test helps to write the correct code in the first place. Personally, I prefer the external-style tests which examine observed behavior of the system. These sorts of tests tend to need less maintenance than unit tests since they are at the API level versus dealing with implementation details.

So in my case I'm writing something which is a maths module, however it is layered, and only the final layer is exposed to the user. As such I do need unit tests on each layer of mathematical abstraction in order the integration tests to be sensible. The logic is not necessarily complex but is very prone to small corner case mistakes, which is why I want to have the unit tests.

2 Likes