In my main RS I have to declare mod tests; in the main.rs file for my unit tests to run. Coming from other languages, my startup file never has had to contain any information about the unit tests. Is this a standard in rust, or is there some configuration I am missing?

My file structure is as such:

src/
-- config/
-- models/
-- services/
-- tests/    <-- this is where my mod.rs for my tests are
main.rs

When running cargo test with a main including:

#[cfg(test)]
mod tests;

my unit tests are found, and they run.

If I remove the mod tests in main, running the same cargo test command does not run my unit tests.

1

There are 1 best solutions below

0
Stargateur On BEST ANSWER

In your case, cargo only care about src/main.rs, so yes you need to tell rust there is a mod call tests.

Cargo have some special file/folders already, there is a tests folder but its need to be in the root of the project path.

Integration tests go in the tests directory. Package Layout

Read more: