Access file during integration-test

Hello everyone

I am learning Rust by writing a little tool which I can pass a config-file. I've written a little (black-box) integration test and placed it within the "tests" folder as well as a sample-config file. Now I would like to access this file relatively. Currently I use

Path::new("../../tests/myfile")

because I've expected the test is started from /target/debug but this doesn't work. Specifying the absolute path works of course.

Coming from Java where Maven and Gadle actually copy the test-resources to the target folder next to the binary, I can easily access the test-resources with a relative path during a test without having to know how the target directory is structured. Is there something similar possible with Cargo? Or do I have to use another API to load the file?

I've read many articles about testing in rust, but none actually covered this integration-test topic with additional resources.
Thanks

The program is started from wherever you ran cargo test, so the path would be tests/myfile if you run it from the main project directory.

For other options, check out Environment variables Cargo sets for crates, e.g. CARGO_BIN_EXE_<name>.

1 Like

Ok, that was simple :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.