Skip to content

Stricter test modules #10506

Closed
Closed
@JonathanWoollett-Light

Description

@JonathanWoollett-Light

What it does

It is easier to illustrate this as a lint formed of multiple lints.

  • test_outside_tests_module: Warns when a unit test is outside a tests module.

  • unflagged_tests_module: Warns when a module named tests is not flagged with #[cfg(test)].

  • non_eof_tests_module: Warns when a module named tests is not at the end of a file.

Lint Name

strict_tests_module

Category

style, pedantic

Advantage

  • Enforces the idiomatic style of unit tests.
  • Allows tools like grcov to correctly exclude unit tests from coverage with --excl-start "mod tests".

Drawbacks

  • Tools like bindgen often include tests outside test modules. This would warn on this case.

Example

#[test]
fn my_test() { /* ... */ }
#[cfg(test)]
mod tests { /* ... */ }

Could be written as:

#[cfg(test)]
mod tests {
    #[test]
    fn my_test() { /* ... */ }
    /* ... */
}

mod tests { /* ... */ }

Could be written as:

#[cfg(test)]
mod tests { /* ... */ }

#[cfg(test)]
mod tests { /* ... */ }
fn an_item_after() { /* ... */ }

Could be written as:

fn an_item_after() { /* ... */ }
#[cfg(test)]
mod tests { /* ... */ }

Metadata

Metadata

Assignees

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions