tenpn
tenpn

Reputation: 4716

Cargo not running tests in top-level file

I'm quite confused by how Cargo finds tests to run.

I've created a Cargo project and it added a main.rs for me. If I add #[test] functions in there, they're found and run with cargo test. If I add a foo.rs file as a sibling to main.rs and add tests in there, they're not found and run.

What subtlety am I missing? Do I somehow have to teach Cargo about new files?

Upvotes: 24

Views: 12651

Answers (1)

Peter Hall
Peter Hall

Reputation: 58875

Cargo will not just compile any files that happen to be in your source directory. In order for Cargo to find a file, it must be referenced as a module either in main.rs/lib.rs or from some sub-module.

For example, in your main.rs:

mod foo;

That's it.

Upvotes: 38

Related Questions