Conditional compilation with `cfg(test)` on dependent crates without effect on when testing parent crate

Hi there,

I do have two crates: parent and child.
The child crate uses some conditional compilation that should only be compiled while testing the crate.

The child crate is maintained as dev-dependency to my parent crate. However, when running the tests on the parent crate the child is not compiled with the test configuration.

I kind of understand why this is the case - as otherwise all unit tests of dependent crates might be run as well while parent is testet, but is there any way to do some conditional compilation checks in the child that are only taken into consideration when compiled/run in test mode ?

The only idea that comes into my mind would be a specific feature flag for child if used in the dev-dependencies like:

child = { path = "../child", features = ["test"] }

is there another more built-in way of achieving this ?

Thanks in advance.

That's actually the way to do it :smile: :

  1. the moment a crate becomes a dependency, the only way to tweak its "profile" / behavior is through feature flags (or env vars + scripts).

  2. And the way to attach a feature flag to a dependency when testing / dev-eloping your app is to use the [dev-dependencies] of the manifest file.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.