rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Test function name starts with `test_`

Open laralove143 opened this issue 2 years ago • 7 comments

What it does

None of the examples in Rust guidelines on writing tests start functions with test_, which makes for better code quality since tests::foo is more readable than tests::test_foo

Somewhat related to: clippy::module_name_repetitions

Lint Name

test_name_repetitions

Category

pedantic

Advantage

Make the code match Rust guidelines thus making it more idiomatic

Drawbacks

Not sure

Example

#[cfg(test)]
mod tests {
    #[test]
    fn test_foo() {
        assert!(true);
    }
}

Could be written as:

rust
#[cfg(test)]
mod tests {
    #[test]
    fn foo() {
        assert!(true);
    }
}

laralove143 avatar Jun 02 '22 09:06 laralove143

I've seen that sometimes the test functions are directly in the module that gets tested and not a nested module. I guess the lint isn't applicable in that case.

mikerite avatar Jun 03 '22 05:06 mikerite

Could you elaborate? Maybe showing a module tree

laralove143 avatar Jun 04 '22 15:06 laralove143

Is anyone looking into this?

laralove143 avatar Jun 21 '22 12:06 laralove143

Could you elaborate? Maybe showing a module tree

Something like that

struct Foo;

// test `Foo` in the middle of the file 
#[cfg(test)]
#[test]
fn test_foo() { todo!() }

struct Bar;

// test `Bar` in the middle of the file 
#[cfg(test)]
#[test]
fn test_bar() { todo!() }

// ...

kraktus avatar Sep 05 '22 12:09 kraktus

Oh I didn't know you could do that, we could just limit this lint for items in a module containing test in its name, kind of like https://rust-lang.github.io/rust-clippy/master/index.html#module_name_repetitions

laralove143 avatar Sep 07 '22 14:09 laralove143