Cargo: What is the purpose of `lib.test = false`?

I've been wondering whether to get rid of unit tests in favor of integration tests in one project recently. One big roadblock is that contributors introducing new unit tests would still be able to run the test suite successfully, but their tests just wouldn't run, without any warnings.

In this context, I had the idea that rustc's new check-cfg machinery could help catching: Mark existing well-known cfg as unknown · Issue #117778 · rust-lang/rust · GitHub. However, it would be incorrect for Cargo to automatically mark the test cfg as unknown in lib.test = false crates because you can still compile and run unit tests in such crates by passing --lib to cargo test.

To me, it has always seemed like lib.test = false is something projects would set when they don't want to have any unit tests. But the actual semantics are that there can still be unit tests, they just don't run by default. So now I'm wondering: what's the idea behind that? Are there projects that have unit tests, yet intentionally set lib.test = false?

you may want to set lib.harness = false which will disable the dependency to the libtest crate and when you run cargo test, it will fail to compile. cargo build is not affected though.

No, the idea isn't that there should be no tests for cargo test to run such that erroring instead works. The idea is to enforce that all tests for the given crate are integration tests (not disabled by lib.test = false) instead of unit tests.

A pragmatic solution would be to set up some checks in CI. You could check for #[test] outside of the integration test directory with some quick grep commands.

At least, in all the projects where I set lib.test = false, the intention is to NOT have unit tests in the crate (in favor of a single, separate "test suite" crate)
A warning or an error preventing contributors from introducing new unit tests would be useful indeed! (Besides the grep approach.)

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.