Cecile
Cecile

Reputation: 1765

How to ignore an example when running tests with cargo?

I'm making a proc_macro crate where I have 2 examples in the directory examples/.

When I run cargo test, the 2 examples are compiled but one of the examples is failing on purpose and it prevents the test from running. I want to make an example that fails to compile to show the user how it works.

According to the doc this behavior is intended:

They must compile as executables (with a main() function) and load in the library by using extern crate <library-name>. They are compiled when you run your tests to protect them from bitrotting.

This is fine but how can I disable the compilation for my failing example?

Upvotes: 5

Views: 1706

Answers (2)

Ben
Ben

Reputation: 3674

It seems that examples are built to "to ensure they compile"

I think you can remove example compiling by just testing the other targets with cargo test --lib --tests

Upvotes: 1

Cecile
Cecile

Reputation: 1765

I found it!

You can disable the automatic discovery of examples by adding autoexamples = false to [package]

Then you can enumerate all the examples yourself in the following way:

[package]
...
autoexamples = false

[[example]]
name = "basic"
path = "examples/basic.rs"

Upvotes: 5

Related Questions