Run tests without examples

Is it possible to do a cargo test without it building the examples files?

I have a crate with around 300 example files and it is slow to compile, link and run them each time I want to run the tests. I'd like to disable them so that they only run when I explicitly run cargo test --examples.

The cargo man page says:

The default behavior can be changed by setting the test flag for the target in the manifest settings.
Setting targets to test = false will stop them from being tested by default.

However, I'm not sure how that is done. I tried variations of the following in Cargo.toml without success:

test = false

test = false

I'm going to guess that this would still build the examples but wouldn't run/test them so it probably isn't what I am looking for anyway since the Cargo book says the following:

Examples are built by cargo test by default to ensure they continue to compile, but they are not tested by default.

Also, for completeness I am aware I can get what I need by doing something like this:

cargo test --tests

But I would still like to know if there is any way to run cargo test without building the examples.

1 Like

You have to do this for each target:

name = "example1"
test = false

name = "example2"
test = false

There is, unfortunately, no way to configure all targets of a type.

You'd probably want cargo test --lib --bins --tests (specifying all the types of targets you have that aren't examples) otherwise the "unit tests" won't be run.

1 Like

Thanks for the reply. That is a reasonable suggestion but it isn't practical in my case with 300 example files.

The cargo test --tests command is complete for my case since --tests includes --lib and I don't have any bins.

Why don't you move the examples to their own crate in your workspace? That way they won't be compiled when testing your main crate, but you'll still be able to do cargo test --workspace to compile everything and run tests.

1 Like

Just a quick summary to close out on this in case anyone else has the same issue.

I raised this behaviour as an issue/feature request against Cargo Cargo #12430. It was closed/rejected in favour of another overarching feature request that would provide the required behaviour: Cargo #6945.

In the meantime I added this to my ~/.cargo/config.toml:

fast_test = "test --test *"

That way I can run the following to get the required behaviour:

$ cargo fast_test

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.