Skip to content

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

Closed
@mjbshaw

Description

@mjbshaw

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions