Integration Test Bin Itself


#1

If I have a bin crate, and I want to run some integration tests on the bin itself (i.e. execute it on the command line with some arguments and check the output), how would I do that within the confines of cargo test? Does cargo test build the executable as well, so I can depend on it being there? Or do I have to always run cargo build before cargo test?


#2

Any news on that?


#3

Yep, cargo test builds all binaries before invoking the tests, so you can call you binary from your integration tests and assert stuff about the output.

The only tricky bit is finding you binary (it may be in target/release or target/debug).

Incidentally, Cargo tests itself using this approach, so we can steal the trick for locating the binary from the Cargo itself!


#4

Another trick which avoids needing to find the exact location of your binary is to invoke cargo run --bin foo directly from your integration tests. You can then communicate with the program via the usual stdin and stdout mechanism that std::process::Command gives you.