Testing 通过使用“功能”标志运行其他测试;货物测试“;

Testing 通过使用“功能”标志运行其他测试;货物测试“;,testing,rust,rust-cargo,Testing,Rust,Rust Cargo,在使用cargo test时,我想忽略一些测试,只有在显式传递特性标志时才会运行这些测试。我知道这可以通过使用#[ignore]和货物测试--ignored来实现,但出于其他原因,我希望有多组被忽略的测试 我试过这个: #[test] #[cfg_attr(not(feature = "online_tests"), ignore)] fn get_github_sample() {} 当我根据需要运行货物测试时,这会被忽略,但我无法让它运行 我尝试了多种方式运行货物,但这些测试仍然被忽略:

在使用
cargo test
时,我想忽略一些测试,只有在显式传递特性标志时才会运行这些测试。我知道这可以通过使用
#[ignore]
货物测试--ignored
来实现,但出于其他原因,我希望有多组被忽略的测试

我试过这个:

#[test]
#[cfg_attr(not(feature = "online_tests"), ignore)]
fn get_github_sample() {}
当我根据需要运行
货物测试时,这会被忽略,但我无法让它运行

我尝试了多种方式运行货物,但这些测试仍然被忽略:

货物测试--具有“在线测试”功能
货物测试-所有功能
然后,我按照将特性定义添加到我的
Cargo.toml
,但它们仍然被忽略

我在货物中使用工作区。我尝试在两个
Cargo.toml
文件中添加功能定义,没有任何区别。

没有工作区 Cargo.toml

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

[features]
network = []
filesystem = []

[dependencies]
[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" }
[workspace]
members = ["feature-tests"]
输出

$cargo测试
运行2个测试
测试文件系统。。。忽略
测试网络。。。忽略
$cargo测试--功能网络
运行2个测试
测试文件系统。。。忽略
测试网络。。。失败
$cargo测试--特性文件系统
运行2个测试
测试网络。。。忽略
测试文件系统。。。失败
(删除一些输出以更好地显示效果)

有工作空间 布局

。
├── 货舱
├── 特性测试
│   ├── 货舱
│   ├── src
│   │   └── 图书馆
├── src
│   └── 图书馆
功能测试
包含上述第一节中的文件

Cargo.toml

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

[features]
network = []
filesystem = []

[dependencies]
[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" }
[workspace]
members = ["feature-tests"]
(删除一些输出以更好地显示效果)

具有虚拟清单的工作区 做。您需要在子项目中运行测试,或者指定相应Cargo.toml的路径

布局

。
├── 货舱
└── 特性测试
├── 货舱
└── src
└── 图书馆
功能测试
包含上述第一节中的文件

Cargo.toml

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

[features]
network = []
filesystem = []

[dependencies]
[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" }
[workspace]
members = ["feature-tests"]
输出

$cargo-test--all--manifest-path功能测试/cargo.toml--features=network
运行2个测试
测试文件系统。。。忽略
测试网络。。。失败
$cargo test--all--manifest路径功能测试/cargo.toml
运行2个测试
测试文件系统。。。忽略
测试网络。。。忽略

(为了更好地显示效果,删除了一些输出)

您是否尝试过在相应的测试中使用
#[cfg(feature=“online_tests”)]
。@Neikos缺点是测试甚至不会被编译。如果有编译错误,在启用这些功能之前,您不会看到它。此外,您不会看到测试输出中存在被忽略的测试,这可能会提醒您启用它们。我知道它们也需要#[test],如果我这样做了,我知道“货物测试”将运行它们否?这不是我想要的。