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

后端 未结 1 1778
忘了有多久
忘了有多久 2020-12-18 22:26

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 #

相关标签:
1条回答
  • 2020-12-18 23:04

    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

    #[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.

    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). You will need to run the tests from within the sub project or specify the path to the appropriate Cargo.toml

    Layout

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

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

    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)

    0 讨论(0)
提交回复
热议问题