Cargo: how do I make a integration test A depend on binary B?

I've got a rust project that currently consists of a single [[bin]] target (but that could trivially be split up into a bin and a lib - does some trivial initialization and then calls a single method in a sub-module).

I'm about to write some code that will involve running a sub-process. The easiest way I can think of to test this is to create a binary that just writes its command-line args and working directory to a temp file. The test can then make the code that should run the sub-process, then check that the output file was created and contains the expected args and working directory.

The problem is how do I tell Cargo "before you run the tests in module A, make sure that you've built binary B". Any suggestions?



This should just work. When running integration tests, Cargo makes sure that binaries are build. In fact, Cargo itself uses this to test itself :slight_smile:

Cool. That makes things easy :slight_smile:


Alas that doesn't seem to work :-(. My cargo toml has this:

name = "find"
path = "src/find/"

name = "testing-commandline"
path = "src/testing/commandline/"

and src/find/ has tests that expect ./target/debug/testing-commandline to exist.

If I run cargo build; cargo test then everything works fine. If I run cargo clean; cargo test then my tests fail because testing-commandline doesn't exist. What am I doing wrong?

I think you need to put your integration style tests at src/tests/foo.

When running unit tests, cargo won't build binaries. You also probably don't want to hard-code debug in the path. See how Cargo handles this:

Doh! I'm an idiot. thanks.

I hadn't actually hard-coded debug (that was a simplification in my mail), but I was using the env::args().next(). env::current_exe is neater :slight_smile: thanks for the tip