Emilia Bopp
Emilia Bopp

Reputation: 883

How do I use a feature of a dependency only for testing?

Say, I have a crate with a dependency that has an optional feature. Now this feature is mostly useful for testing, but the crate itself is a dependency for the whole code. Is it possible to instruct cargo to use the feature only for testing?

In my concrete example the optional feature depends on quickcheck, which I do not necessarily want to make a mandatory dependency for users of my crate.

Upvotes: 15

Views: 4713

Answers (1)

BurntSushi5
BurntSushi5

Reputation: 15374

You can use a feature for a development dependency just like you would for regular dependencies. In the case of quickcheck, its only feature is collect_impls, so you can add this to your Cargo.toml:

[dev-dependencies.quickcheck]
version = "*"
features = ["collect_impls"]

N.B. This was actually done wrong inside of quickcheck. I just fixed it in 0.1.29.

Upvotes: 10

Related Questions