简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM