-
Notifications
You must be signed in to change notification settings - Fork 731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proc-macros: #[traced_test] macro #201
Comments
What is the boilerplate that is currently needed? I need some logs in one of my tests right now and I'm not sure how to set it up. Also, will |
Essentially, you would want something like this: #[test]
fn my_test() {
let _guard = tracing_subscriber::fmt()
// any additional configuration of the subscriber you might want here..
.set_default();
} If you have tests involving multiple threads, you would probably want to share the subscriber with all those threads and have them all call If you use a global default subscriber for tests, you would probably want each test to have a span with the test's name, so you can see which test an individual log came from. Something like this: #[test]
fn my_test() {
// ignore the result this returns; as it will fail if another test has already created
// the global default subscriber
let _ = tracing_subscriber::fmt()
// any additional configuration of the subscriber you might want here..
.try_init();
let span = tracing::info_span!("my_test");
let _enter = span.enter();
// ... the rest of your test here ...
} Even if we're not setting a global subscriber, we might still find it valuable to generate a span for the test name so that it's displayed in the log output if multiple tests are running at once.
This depends on how the proc-macro is implemented. I think it could definitely be written in a way that would remain compatible with other attribute macros that generate tests, though this might be ordering-dependent. As a side note, you may want to look at the |
I remember taking a stab at this issue when I was looking into Rust and proc macros a number of lifetimes ago. I'll leave it here for someone willing to pull it through :) https://github.com/eHammarstrom/tracing/blob/master/tracing-proc-macros/src/lib.rs |
@eHammarstrom thanks, that's a neat prototype! I wonder if, rather than taking the type-name of a |
This would be great. Any ideas how |
You just dump the rest of the attrs out: use quote::quote;
pub fn test_attr(input: syn::ItemFn) -> proc_macro2::TokenStream {
let { attrs, vis, sig, block } = &input;
let test_attr = sig.asyncness.as_ref()
.map(|_| quote!(#[tokio::test]))
.unwrap_or_else(|| quote!(#[test]);
quote! {
#test_attr
#(#attrs)*
#vis #sig {
crate::init_tracing();
#block
}
}
} |
I just published the tracing-test crate. Maybe that resolves this issue for some of you? |
Feature Request
It's common to want to use tracing in tests because who doesn't want to use tracing everywhere. Right now this adds an extra bit of boiler plate to every test. Also it's not widely known (I think?) that tests in the same file run in multiple threads of the same process, so the global subscriber is shared between all tests. This is probably fine but it would be better for tests to use scoped subscribers for each test.
Crates
tracing-proc-macros
Motivation
Discussion with lucio and eliza on gitter
Proposal
We should introduce a new macro that takes the place of the
#[test]
macro and will expand to a test macro and the body of the function inside of a default scoped logger. In the future we can look into ways to configure the implicitly setup logger if such needs arise.The text was updated successfully, but these errors were encountered: