Skip to content

#[test] functions are silently excluded when they're defined inside of another function #50506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mjbshaw opened this issue May 7, 2018 · 1 comment

Comments

@mjbshaw
Copy link
Contributor

mjbshaw commented May 7, 2018

Consider the following program:

pub fn main() {
    #[cfg(test)]
    #[test]
    fn it_works() {
        panic!("Proof this test doesn't run");
    }
}

I expect cargo test to run the unit test it_works. But this doesn't happen. cargo test doesn't run any tests:

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Additionally, rustc emits no warnings or errors when compiling this. The test is just silently excluded from the executable (verified by running otool and grepping for it_works).

rustc shouldn't exclude this test from the executable, and cargo test should run this test.

You might wonder why anyone would add a unit test within a function. My use case is a unit test injected by an attribute macro on a fn within an impl block (the attribute does many things to the function and adds the unit test to verify some parts that can't be validated at compile time).

Tested with rustc 1.27.0-nightly (91db9dcf3 2018-05-04) and cargo 1.27.0-nightly (af3f1cd29 2018-05-03).

@estebank
Copy link
Contributor

estebank commented May 7, 2018

Thanks for the report, this is indeed bad. Duplicate of #36629. Closing in lieu of that one, as it already has a discussion going on.

@estebank estebank closed this as completed May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants