I have the following code defining a path where generated files can be placed:
fn gen_test_dir() -> tempdir::TempDir {
tempdir::TempDir::new_in(Path::new("/tmp"), "filesyncer-tests").unwrap()
}
This function is defined in tests/lib.rs
, used in the tests in that file and I would also like to use it in the unit tests located in src/lib.rs
.
Is this possible to achieve without compiling the utility functions into the non-test binary and without duplicating code?
src/lib.rs
and then use it intests/lib.rs
?#[test]
to avoid compiling into release-binary and received this error: "functions used as tests must have signature fn() -> ()"#cfg(not(test))
that will remove your code, during non-test phases.#[cfg(test)]
. I tend to have a#[cfg(test)] mod tests
in my lib.rs (or submodules), into which I put all the unit test functions and any utilities they need.