Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 829
Emilia Bopp Avatar asked Jan 10 '15 01:01

Emilia Bopp


People also ask

What is a test dependency?

What is Dependency Testing? Dependency Testing, a testing technique in which an application's requirements are pre-examined for an existing software, initial states in order to test the proper functionality. The impacted areas of the application are also tested when testing the new features or existing features.

What does cargo test do?

It extracts code samples from documentation comments of the library target, and then executes them. Different from normal test targets, each code block compiles to a doctest executable on the fly with rustc .


1 Answers

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.

like image 185
BurntSushi5 Avatar answered Oct 23 '22 14:10

BurntSushi5