Run additional tests using function flag for β€œload test”

I have some tests that I would like to ignore when using cargo testand run only when the function flag is explicitly skipped. I know this can be done using #[ignore]and cargo test -- --ignored, but I would like to have several sets of ignored tests for other reasons.

I tried this:

#[test]
#[cfg_attr(not(feature = "online_tests"), ignore)]
fn get_github_sample() {}

This is ignored when I run cargo testas desired, but I cannot get it to work.

I tried several ways to launch Cargo, but the tests continue to be ignored:

cargo test --features "online_tests"

cargo test --all-features

Then I added a function definition to Cargo.toml this page , but they continue to be ignored.

Cargo. Cargo.toml .

+7
1

Cargo.toml

[package]
name = "feature-tests"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]

[features]
network = []
filesystem = []

[dependencies]

SRC/lib.rs

#[test]
#[cfg_attr(not(feature = "network"), ignore)]
fn network() {
    panic!("Touched the network");
}

#[test]
#[cfg_attr(not(feature = "filesystem"), ignore)]
fn filesystem() {
    panic!("Touched the filesystem");
}

$ cargo test

running 2 tests
test filesystem ... ignored
test network ... ignored

$ cargo test --features network

running 2 tests
test filesystem ... ignored
test network ... FAILED

$ cargo test --features filesystem

running 2 tests
test network ... ignored
test filesystem ... FAILED

( , )

.
β”œβ”€β”€ Cargo.toml
β”œβ”€β”€ feature-tests
β”‚   β”œβ”€β”€ Cargo.toml
β”‚   β”œβ”€β”€ src
β”‚   β”‚   └── lib.rs
β”œβ”€β”€ src
β”‚   └── lib.rs

feature-tests .

Cargo.toml

[package]
name = "workspace"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]

[features]
filesystem = ["feature-tests/filesystem"]
network = ["feature-tests/network"]

[workspace]

[dependencies]
feature-tests = { path = "feature-tests" }

$ cargo test --all

running 2 tests
test filesystem ... ignored
test network ... ignored

$ cargo test --all --features=network

running 2 tests
test filesystem ... ignored
test network ... FAILED

( , )

( β„– 4942). Cargo.toml

.
β”œβ”€β”€ Cargo.toml
└── feature-tests
    β”œβ”€β”€ Cargo.toml
    └── src
        └── lib.rs

feature-tests .

Cargo.toml

[workspace]
members = ["feature-tests"]

$ cargo test --all --manifest-path feature-tests/Cargo.toml --features=network 

running 2 tests
test filesystem ... ignored
test network ... FAILED

$ cargo test --all --manifest-path feature-tests/Cargo.toml

running 2 tests
test filesystem ... ignored
test network ... ignored

( , )

+4

All Articles