if you need to test private items, you need to write a unit test.
And I also don't want it to run because it would be trying to connect to some random IP address when network access may not be available, but it still should compile.
I couldn't find anything for "no_run", "compile-only", "compile only" in the Writing Automated Tests section of The Book, or Rust by Example's Unit testing.
You can write normal function for it. Compiler never let the non-compilable function lives within the source code.
#[cfg(test)] // prevents this compile-only test function never be shipped within executable binary
#[allow(dead_code)] // prevents to emit dead code warning
fn bar() -> i32 {
3
}
That's a clever way to write a #[test] that's not actually one. Thanks! Will wait a bit more to see if there are better options, else I'll mark it as solution.
The problem with that is that it will be ran by cargo test -- --ignored, and I may have other tests under #[ignore] that I want to run sometimes but not always, as described in Ignoring Some Tests Unless Specifically Requested. This one should always be compile-only, and not run then.
Then @Hyeonu's solution is the best one. You could also stuff all those compile only tests into a module marked #[cfg(test)] to reduce the boilerplate.
Doesn't the Rust compiler ignore dead code for at least some checks? At least I remember seeing something like that in the issues of the Rust repository, but maybe compilation is always done, just not those certain checks. Not sure if only for dead branches or entire functions, or then the other approach wouldn't work either now that I think about it.
Not sure if this is the issue I was thinking of, but Dead code is not checked is somewhat relevant. It compiles with a (contrived) if false but doesn't without it.
Yeah, but there's a reason you need a contrived if false, and that just using if false doesn't trigger it: In that case simply looking at the control graph of the code illuminates the problem, but if false does not produce such a control graph, and does not cause the never type to crop up, which is the source of unreachable_code warnings.
An actual if false is not removed until you reach optimization, which is after any type or borrow checking.