Closed flokli closed 7 months ago
Can you just check if it works if you don't use template
? I guess it doesn't :cry:
Maybe the compiler process cfg
attrs before the rstest's procedural macros. I guess that I can introduce a #[rstest::cfg(...)]
macro that it's expanded in #[cfg(...)]
, but that's my last resource. I need to understand better what's happening.
Without #[template]
, the #[cfg(feature= "…]
statement also applies to the entire block:
#[rstest]
#[case::grpc(make_grpc_directory_service_client().await)]
#[case::memory(directoryservice::from_addr("memory://").await.unwrap())]
#[case::sled(directoryservice::from_addr("sled://").await.unwrap())]
#[cfg(feature = "cloud")]
#[case::bigtable(make_bigtable().await)]
#[tokio::test]
async fn pingpong(#[case] directory_service: impl DirectoryService) {
let resp = directory_service.get(&DIRECTORY_A.digest()).await;
assert!(resp.unwrap().is_none())
}
This either runs all tests if the "cloud" feature flag is set, or none (if it's not set).
Priorities of procedural macros is something I didn't expose myself with yet, so sorry for not being a lot of help :see_no_evil:
Ok, you should use cfg_attr
instead:
#[rstest]
#[case::grpc(make_grpc_directory_service_client().await)]
#[case::memory(directoryservice::from_addr("memory://").await.unwrap())]
#[case::sled(directoryservice::from_addr("sled://").await.unwrap())]
#[cfg_attr(feature = "cloud", case::bigtable(make_bigtable().await))]
#[tokio::test]
async fn pingpong(#[case] directory_service: impl DirectoryService) {
let resp = directory_service.get(&DIRECTORY_A.digest()).await;
assert!(resp.unwrap().is_none())
}
That should work also with #[template]
/#[apply(...)]
Thanks! Indeed this works, also for template/apply!
Let me check if there's a good place to document this (maybe somewhere close to test cases).
I opened https://github.com/la10736/rstest/pull/236, PTAL!
I use
rstest
to run a bunch of "acceptance tests" across different implementations all exposing the same trait.I use
rstest_reuse
's#[template]
and#[apply(…)]
to write a template containing all implementations I want to test with (as test cases), and useapply
for each individual test:This keeps the amount of boilerplate for each test case to a minimum, thanks a lot :-)
Now I want to add an additional store implementation, but its code is behind a feature flag.
It seems it's not possible to have a single
#[case:…]
with a#[cfg(feature = "cloud")]
- it doesn't apply to the macro(s), but entirely enables/disables the wholepub fn directory_services
template function:Currently my only way out seems to be having two implementations of
pub fn directory_services
, one without, and one with the feature flag:This obviously won't scale when feature flags get more (granular). Any thoughts on how to do this? Maybe adding an optional argument, allowing to express a cfg!() expression for when a case should get included?