Search code examples
rustintegration-testingrust-cargo

Use rust cargo to run tests in workspace root


I've got the following rust project layout:

project_name
 ├── crate_1
 │     ├── src
 │     │     ...
 │     │     └── main.rs
 │     └── Cargo.toml
 ├── crate_2
 │     ├── src
 │     │     ...
 │     │     └── lib.rs
 │     └── Cargo.toml
 ├── tests
 │     └── tests.rs <-- run tests in here
 └── Cargo.toml

I want to run the tests in the tests directory using cargo, however cargo can't seem to find them. Is there a way to get cargo to run them?


Solution

  • tokio is a very good example.

    Now you already have a tests directory, let's add it to the members in workspace Cargo.toml.

    [workspace]
    
    members = [
        "crate1",
        "crate2",
    
        "tests"
    ]
    

    We assume that there are two integration test files, test_crate1.rs and test_crate2.rs under the tests directory.

    Create a Cargo.toml under the tests directory with these contents:

    [package]
    name = "tests"
    version = "0.1.0"
    edition = "2021"
    publish = false
    
    [dev-dependencies]
    crate1 = { path = "../crate1" }
    crate2 = { path = "../crate2" }
    
    [[test]]
    name = "test_crate1"
    path = "test_crate1.rs"
    
    [[test]]
    name = "test_crate2"
    path = "test_crate2.rs"
    

    Run cargo test in workspace directory to check it.