简体   繁体   English

通过使用功能标记进行“货运测试”来运行其他测试

[英]Run additional tests by using a feature flag to “cargo test”

I have some tests that I would like to ignore when using cargo test and only run when explicitly passed a feature flag. 我有一些在使用cargo test时要忽略的cargo test ,只有在明确通过功能标志时才运行。 I know this can be done by using #[ignore] and cargo test -- --ignored , but I'd like to have multiple sets of ignored tests for other reasons. 我知道这可以通过使用#[ignore]cargo test -- --ignored ignored来完成,但是出于其他原因,我想拥有多组被忽略的测试。

I have tried this: 我已经试过了:

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

This is ignored when I run cargo test as desired, but I can't get it to run. 当我根据需要运行cargo test时,将忽略此操作,但是我无法使其运行。

I have tried multiple ways of running Cargo but the tests continue to be ignored: 我尝试了多种运行Cargo的方法,但测试仍然被忽略:

cargo test --features "online_tests"

cargo test --all-features

I then added the feature definition into my Cargo.toml as per this page , but they continue to be ignored. 然后,按照该页面的说明 ,将功能定义添加到我的Cargo.toml中,但它们仍然被忽略。

I am using workspaces in Cargo. 我在Cargo中使用工作区。 I tried adding the feature definition in both Cargo.toml files with no difference. 我尝试在两个Cargo.toml文件中添加功能定义,没有区别。

Without a workspace 没有工作区

Cargo.toml 货物清单

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

[features]
network = []
filesystem = []

[dependencies]

src/lib.rs 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");
}

Output 输出量

$ 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

(some output removed to better show effects) (删除了一些输出以更好地显示效果)

With a workspace 带工作区

Layout 布局

.
├── Cargo.toml
├── feature-tests
│   ├── Cargo.toml
│   ├── src
│   │   └── lib.rs
├── src
│   └── lib.rs

feature-tests contains the files from the first section above. 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" }

Output 输出量

$ 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

(some output removed to better show effects) (删除了一些输出以更好地显示效果)

With a workspace with a virtual manifest 使用带有虚拟清单的工作区

Virtual manifests do not support specifying features (Cargo issue #4942) . 虚拟清单 不支持指定功能(货运问题#4942) You will need to run the tests from within the sub project or specify the path to the appropriate Cargo.toml 您将需要在子项目中运行测试,或指定适当的Cargo.toml的路径。

Layout 布局

.
├── Cargo.toml
└── feature-tests
    ├── Cargo.toml
    └── src
        └── lib.rs

feature-tests contains the files from the first section above. feature-tests包含上面第一部分中的文件。

Cargo.toml 货物清单

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

Output 输出量

$ 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

(some output removed to better show effects) (删除了一些输出以更好地显示效果)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM