testingrustrust-cargo

Is it possible to make a step-by-step build using cargo?


I have a multi-workspace cargo project:

cli/
server/
test/
Cargo.toml

In parent Cargo.toml

[workspace]
members = [
    "server",
    "cli",
    "tests",
]
resolver = "1"

In test workspace a have a bunch of integration tests which are depends on build of cli crate. So, I just want to run cargo test to run all my tests that are depends on builded cli executable file. But now it's fails with:

test tests::should_output_help ... FAILED

failures:

---- tests::should_output_help stdout ----
Error: Cause: Cargo command not found: /Users/l3r8y/code/fakehub/target/debug/cli

But if i'll run cargo build before tests everything would be fine. So question is – Is it possible to make cargo test build cli create and run tests only after cli has been built?


Solution

  • Cargo has special support for the tests of a specific package accessing binaries from that package. It does not yet have anything for using binaries from a separate package (that would be artifact dependencies, which are not implemented yet). Therefore:

    1. Move your relevant tests from the test/ package to cli/tests/ (the integration test location for the cli package).
    2. In your tests, use env!("CARGO_BIN_EXE_cli") as the path to the command to execute. This environment variable is automatically set, when a test is being compiled or run, with the path to each binary in the same package. (Docs)