How to find resources for testing with Cargo?

I am working on a project with files, and I would like to use text files to test my work. However, tests do not run from the tests/ directory, and therefore I cannot reliably find them when I execute cargo run .

Does Cargo handle this, always running a test from the root directory (which seems to be the case, but I did not find anything to confirm)?

+9
source share
1 answer

The environment variable CARGO_MANIFEST_DIR can give you a stable base point for linking to other files. Here we assume that the resources/test directory is at the top level of the box:

 use std::path::PathBuf; fn main() { let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR")); d.push("resources/test"); println!("{}", d.display()); } 

See also:

+20
source

All Articles