I have utils.rs with some functions. I want to test each function with some input and expected output, one assertion per test with a descriptive name. I could prefix tests with name of the function being tested, but would prefer some "standard" way of grouping/scoping the tests.
Does there exist some convention for grouping tests? I did some searching, and all I found was stainless. Was hoping to group tests without extra dependencies.
// in <Cargo.toml Dir>/tests/util_test.rs
use my_crate::utils::*;
#[test]
fn my_integration_test() {
//This is meant to test from the point of view from the crate user
}
Regarding imports for testmodules, I always "carry the use super::*;". It is a simple practice that always works and is robust to moving the mods around afterwards:
#[cfg(test)]
mod split_arguments_tests {
use super::*;
mod split_by {
use super::*;
#[test]
fn space ()
{
assert_eq!(split_arguments("a b"), vec!["a", "b"]);
}
#[test]
fn tab ()
{
assert_eq!(split_arguments("a\tb"), vec!["a", "b"])
}
}
mod trimming {
use super::*;
#[test]
fn spaces ()
{
assert_eq!(split_arguments(" a b "), vec!["a", "b"]);
}
}
}
There's no magic in the mod tests name. You can put as many test modules as you want under any given module. For instance, to keep all tests for a given function grouped, I might do: