本文介绍了通过使用功能标志“货物测试"运行额外的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些在使用 cargo test 时想忽略的测试,并且仅在显式传递功能标志时运行.我知道这可以通过使用 #[ignore]cargo test -- --ignored 来完成,但由于其他原因我想要多组被忽略的测试.

I have some tests that I would like to ignore when using cargo test and only run when explicitly passed a feature flag. 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.

我已经试过了:

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

当我根据需要运行 cargo test 时,这会被忽略,但我无法让它运行.

This is ignored when I run cargo test as desired, but I can't get it to run.

我尝试了多种运行 Cargo 的方法,但测试仍然被忽略:

I have tried multiple ways of running Cargo but the tests continue to be ignored:

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 中使用工作区.我尝试在两个 Cargo.toml 文件中添加功能定义,但没有任何区别.

I am using workspaces in Cargo. I tried adding the feature definition in both Cargo.toml files with no difference.

推荐答案

没有工作区

Cargo.toml

[package]
name = "feature-tests"
version = "0.1.0"
authors = ["An Devloper <[email protected]>"]

[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

(为了更好的展示效果,移除了一些输出)

(some output removed to better show effects)

布局

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

feature-tests 包含来自上面第一部分的文件.

feature-tests contains the files from the first section above.

Cargo.toml

[package]
name = "workspace"
version = "0.1.0"
authors = ["An Devloper <[email protected]>"]

[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

(为了更好的展示效果,移除了一些输出)

(some output removed to better show effects)

虚拟清单不支持指定功能(货物问题 #4942).您需要从子项目中运行测试或指定适当 Cargo.toml 的路径

Virtual manifests do not support specifying features (Cargo issue #4942). You will need to run the tests from within the sub project or specify the path to the appropriate Cargo.toml

布局

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

feature-tests 包含来自上面第一部分的文件.

feature-tests contains the files from the first section above.

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

(为了更好的展示效果,移除了一些输出)

(some output removed to better show effects)

这篇关于通过使用功能标志“货物测试"运行额外的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 11:33