steve_pineapple
steve_pineapple

Reputation: 127

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?

Upvotes: 8

Views: 4966

Answers (2)

Daniel Cooke
Daniel Cooke

Reputation: 1634

If your project is a single distributable package, comprised of several workspace crates, and you want to run integration tests from a root tests directory.

You don't need to specify the tests directory as a workspace member.

Have a look at how clap does it

If you tell Cargo that your root Cargo.toml is infact a package itself, it will treat the root tests directory as normal

[package]
name = "your package"
version = "0.0.0"

Upvotes: 1

Sprite
Sprite

Reputation: 3763

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.

Upvotes: 18

Related Questions