Reliably getting name of currently running test with RUST_TEST_THREADS=1

I have a test framework that saves information into a file based on the name of the test. During CI runs, instead of overwriting the file, it asserts that the data it would have written is identical to what's already in the file. (pretty sure there's a name for this, as well as some existing rust utilities to do this - but suffice it to say we built our own)

Anyway, I'm currently getting a unique-ish file path, based on the name of the test - which I'm reading from the thread name via std::thread::current().name().unwrap(). This works most of the time because the rust test framework names the threads it spawns for executing tests after the test itself.

However, this breaks down in the presence of the env var RUST_TEST_THREADS=1, where rust leaves the thread name unchanged. This leads to all tests accidentally colliding on name of the main thread, "main".

Is there an API that I can use to read the name of the currently-running test?

I don't think there's a good solution for this yet, but there was a recent discussion of the problem on the internals forum: Discovering the current test (name) - libs - Rust Internals

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.