Using different features for regular build and test build


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/

pub fn feature_foo_enabled() -> bool {

And a crate named bin with the contents of src/

extern crate lib;

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

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

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


You can add the same dependency as both a normal and dev dependency to add features for tests:


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


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


Filed as