"cargo test" - test in the main function - no warning


fn f() -> u32 {
   0
}


fn main() {
   #[cfg(test)]
   mod tests {
       use super::f;


       #[test]
       fn test_f() {
           assert_eq!(f(), 1);
       }
   }
}

This code compiles fine.
Running “cargo test” skips the test case “test_f”.

Moving the test case outside of main fixes the issue and we get an expected error message.

Questions:
Why is having a test case in the “main” a bad idea?
Why is there no warning or error message when compiling the code?

Here is a YouTube video recording of the "problem" - https://youtu.be/FyAhU2xavfU

Such tests cannot be run because inner functions like that cannot be named by anything outside of main, including the test harness.

There is supposed to be a warning — Make test harness lint about unnnameable tests. by cjgillot · Pull Request #114414 · rust-lang/rust · GitHub — but you're not getting a warning. So, this is a bug. It seems that it is because of the mod being involved. I have filed a bug report:

4 Likes

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.