I have Rust project with both integration tests (in the /tests
dir) and benchmarks (in the /benches
dir). There are a couple of utility functions that I need in tests and benches, but they aren't related to my crate itself, so I can't just put them in the /utils
dir.
What is idiomatic way to handle this situation?
You could add those utility-functions to a
pub
-module inside your main crate and use the#[doc(hidden)]
or#![doc(hidden)]
attribute to hide them from the docs-generator. Extra comments will guide the reader to why they are there.Create a shared crate (preferred)
As stated in the comments, create a new crate. You don't have to publish the crate to crates.io. Just keep it as a local unpublished crate inside your project and mark it as a development-only dependency:
Cargo.toml
utilities/src/lib.rs
tests/integration.rs
A test-only module
You could place a module inside your crate that is only compiled when a specific feature is passed. This is the same concept used for unit tests. This has the advantage that it can access internals of your library code. It has the disadvantage that you need to pass the flag each time you run the code.
Cargo.toml
src/lib.rs
tests/integration.rs
execution
Use a module from an arbitrary file path
This is just ugly to me, and really goes out of the normal path.
utilities.rs
tests/integration.rs
See also: