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

时间:2018-02-02 12:46:54

标签: testing rust rust-cargo

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

我试过这个:

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

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

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

cargo test --features "online_tests"

cargo test --all-features

然后我按照this page将功能定义添加到我的Cargo.toml,但它们仍会被忽略。

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

1 个答案:

答案 0 :(得分:4)

没有工作区

<强> 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

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

相关问题