I would like the name of the test that is running.
#[traced_test]
#[tokio::test]
async fn foo() {
assert!(true);
println!("test name is: {}", ???);
}
How do I get the test's name?
It should also work in all functions that the test calls:
async fn bar() {
println!("bar: test name is: {}", ???);
}
async fn foo() {
bar().await;
}
where "bar: test name is: foo" should be printed. Or "bar: test name is: <mod>::foo".
The first option you tried working.
You just need to show output:
cargo test -- --nocapture
<- this is the change
cargo.toml :
[dependencies]
tokio = {version = "1.40.0", features = ["full"]}
tracing = "0.1.40"
tracing-test = "0.2.5"
main.rs :
use tracing_test::traced_test;
#[traced_test]
#[tokio::test]
async fn foo() {
bar();
}
fn bar() {
let thread = std::thread::current();
println!("{}", thread.name().unwrap());
}
// Avoid - just to cancel warnings
fn main() {
bar();
}
Output:
test foo ... ok
successes:
---- foo stdout ----
foo
successes:
foo
Edit for the last question edit:
use function_name::named;
use tracing_test::traced_test;
#[traced_test]
#[tokio::test]
#[named]
async fn foo() {
let test_name = function_name!();
println!("test name is: {}", test_name);
assert!(true);
}
Result:
test name is: foo