运行 使用功能标志进行额外测试 "cargo test"
Run additional tests by using a feature flag to "cargo test"
我想在使用 cargo test
时忽略一些测试,而在明确传递功能标志时仅使用 运行 。我知道这可以通过使用 #[ignore]
和 cargo test -- --ignored
来完成,但出于其他原因我想要多组忽略的测试。
我试过这个:
#[test]
#[cfg_attr(not(feature = "online_tests"), ignore)]
fn get_github_sample() {}
当我按要求运行 cargo test
时,这个被忽略了,但是我无法得到运行。
我尝试了多种 运行ning Cargo 方法,但测试仍然被忽略:
cargo test --features "online_tests"
cargo test --all-features
然后我按照 this page 将特征定义添加到我的 Cargo.toml
中,但它们继续被忽略。
我在 Cargo 中使用工作空间。我尝试在两个 Cargo.toml
文件中添加特征定义,没有任何区别。
没有工作空间
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
(为了更好的显示效果去掉了一些输出)
使用带有虚拟清单的工作区
Virtual manifests do not support specifying features (Cargo issue #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
(为了更好的显示效果去掉了一些输出)
我想在使用 cargo test
时忽略一些测试,而在明确传递功能标志时仅使用 运行 。我知道这可以通过使用 #[ignore]
和 cargo test -- --ignored
来完成,但出于其他原因我想要多组忽略的测试。
我试过这个:
#[test]
#[cfg_attr(not(feature = "online_tests"), ignore)]
fn get_github_sample() {}
当我按要求运行 cargo test
时,这个被忽略了,但是我无法得到运行。
我尝试了多种 运行ning Cargo 方法,但测试仍然被忽略:
cargo test --features "online_tests"
cargo test --all-features
然后我按照 this page 将特征定义添加到我的 Cargo.toml
中,但它们继续被忽略。
我在 Cargo 中使用工作空间。我尝试在两个 Cargo.toml
文件中添加特征定义,没有任何区别。
没有工作空间
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
(为了更好的显示效果去掉了一些输出)
使用带有虚拟清单的工作区
Virtual manifests do not support specifying features (Cargo issue #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
(为了更好的显示效果去掉了一些输出)