diff --git a/Cargo.toml b/Cargo.toml index 2f7f1f1..55491cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,4 +23,5 @@ sysinfo = { version = "0.38", default-features = false, features = ["system"] } whoami = "2" [dev-dependencies] +rstest = "0.26.1" tempfile = "3" diff --git a/src/lib.rs b/src/lib.rs index f0e08a6..832e7c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -119,3 +119,44 @@ where }); func(test_dir) } + +#[cfg(test)] +mod test_rstests { + use rstest::rstest; + + use super::*; + + #[rstest] + #[case(1)] + #[case(2)] + fn test_1(#[case] _num: u8) { + let dir = testdir!(); + println!("Created tmp_dir {}", dir.display()); + let mut parts = dir.components(); + match parts.next_back().map(|c| c.as_os_str().to_str().unwrap()) { + Some("case_1") | Some("case_2") => (), + _ => panic!("wrong case directory"), + } + match parts.next_back().map(|c| c.as_os_str().to_str().unwrap()) { + Some("test_1") => (), + _ => panic!("wrong test directory"), + } + } + + #[rstest] + #[case(1)] + #[case(2)] + fn test_2(#[case] _num: u8) { + let dir = testdir!(); + println!("Created tmp_dir {}", dir.display()); + let mut parts = dir.components(); + match parts.next_back().map(|c| c.as_os_str().to_str().unwrap()) { + Some("case_1") | Some("case_2") => (), + _ => panic!("wrong case directory"), + } + match parts.next_back().map(|c| c.as_os_str().to_str().unwrap()) { + Some("test_2") => (), + _ => panic!("wrong test directory"), + } + } +} diff --git a/src/private.rs b/src/private.rs index f2353fc..37e6e44 100644 --- a/src/private.rs +++ b/src/private.rs @@ -186,15 +186,19 @@ fn cargo_pid() -> Option { pub fn extract_test_name(module_path: &str) -> String { let mut name = std::thread::current() .name() - .expect("Test thread has no name, can not find test name") + .unwrap_or_default() .to_string(); if name == "main" { name = extract_test_name_from_backtrace(module_path); } - if let Some(tail) = name.rsplit("::").next() { + if let Some((_head, tail)) = name.split_once("::") { + // The test name usually starts with the module name, so skip that. name = tail.to_string(); } - name + // When using rstest the test name still contains multiple module paths entries, they + // are named "test_name::case_name". So we turn the name into several path entries: + // "test_name/case_name". + name.replace("::", ::std::path::MAIN_SEPARATOR_STR) } /// Extracts the name of the currently executing tests using [`backtrace`].