Question for all the @rustlang hackers out there -- Do you prefer #[cfg(test)] stuff in your crate, or code in the tests/*.rs files for testing the majority of your code when your library is almost entirely public API?

Jan 20, 2020 ยท 10:21 PM UTC

13
2
1
14
Code in tests/. This format is also compatible with #Python and other languages
1
I personally Co-locate when possible. I prefer /tests for larger tests that might require some boilerplate or framework to execute which could be shared.
1
The first option exploits modularity feature you have in rust and don't often find in other languages, module level visibility scoping for tests. It's often better to limit your pub visibility to keep a small and stable surface area. #[cfg(test)] enables this.
The cognative overhead of maintaining narrowly scoped domain in less files (one) is also a plus for me.
I use both. The more complex tests live in tests/. The simple unit tests live next to the code they're testing.