Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Latest commit

 

History

History
47 lines (35 loc) · 825 Bytes

File metadata and controls

47 lines (35 loc) · 825 Bytes

Tests

Tests should be in a tests module, in the same file they're testing. They can be moved to a separate submodule file if necessary.

Parent module members should be imported with use super::* at the top of the submodule.

Example

Don't do:

fn to_test(param: u8) -> u8 {
    param
}

#[test]
fn testing() {
    assert_eq!(to_test(3), 3);
}

Do instead:

fn to_test(param: u8) -> u8 {
    param
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn testing() {
        assert_eq!(to_test(3), 3);
    }
}

Explanation

Having a submodule for tests ensures no unused code warnings are emitted when building the project. The shorthand of use super::* as the first line of the tests module makes it possible to write tests without worrying about being in a separate module.