Implicitly enable feature only for tests

What's the current state of the art if you want to:

  • Implicitly enable a feature for cargo test
  • Allow user to explicitly disable features when running cargo test

.. ?

There's a cargo issue post about this that advocates for:

[dev-dependencies]
self-package-name = { path = ".", default-features = false, features = ["desired_feature"] }

Unfortunately this would include the feature for examples and benchmarks.

Is there a way to implicitly enable a feature (by default) only for cargo test, yet allow it to be explicitly disabled?

I could reverse the logic in the tests, and have a --features=disable-blah -- but I'm trying to follow the principle of enabling features with --features, and not disabling them. (Unless there's very good reason).

Cargo features are not the tool for this; you'll need #[cfg(test)].

1 Like