Using different features for regular build and test build


#1

I want to enable different features of dependencies in when using cargo build and when using cargo test. How would I go about this? Is this even supported?

Say I have a crate named lib with the contents of src/lib.rs:

pub fn feature_foo_enabled() -> bool {
    cfg!(feature="foo")
}

And a crate named bin with the contents of src/main.rs:

extern crate lib;

fn main() {
    println!("Foo enabled: {}",lib::feature_foo_enabled());
}

#[test]
fn test() {
    println!("Foo enabled: {}",lib::feature_foo_enabled());
    assert!(false)
}

How do I make these output something different on cargo run and cargo test?


#2

You can add the same dependency as both a normal and dev dependency to add features for tests: https://github.com/sfackler/rust-postgres-array/blob/master/Cargo.toml#L18


#3

This seems to result in those features always being included, even for the non-dev version.


#4

Er, that’s unfortunate. @alexcrichton that seems like a bug?


#5

Filed as https://github.com/rust-lang/cargo/issues/2596


#6

Thanks!