-
Notifications
You must be signed in to change notification settings - Fork 49
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
Support custom async runner #85
Comments
I'm currently taking a look at how it could work with I currently have this patch for actix: diff --git a/actix-macros/src/lib.rs b/actix-macros/src/lib.rs
index b6a57b9..a016fef 100644
--- a/actix-macros/src/lib.rs
+++ b/actix-macros/src/lib.rs
@@ -58,6 +58,7 @@ pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(item as syn::ItemFn);
let ret = &input.sig.output;
+ let inputs = &input.sig.inputs;
let name = &input.sig.ident;
let body = &input.block;
let attrs = &input.attrs;
@@ -81,7 +82,7 @@ pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
let result = if has_test_attr {
quote! {
#(#attrs)*
- fn #name() #ret {
+ fn #name ( #inputs ) #ret {
actix_rt::System::new("test")
.block_on(async { #body })
}
@@ -90,7 +91,7 @@ pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
quote! {
#[test]
#(#attrs)*
- fn #name() #ret {
+ fn #name ( #inputs ) #ret {
actix_rt::System::new("test")
.block_on(async { #body })
} which makes it almost work. If I amend the code such that it does not emit the ... which makes me come to a question: would you accept a patch where #[test]
#[rstest]
fn foo() {} I think it would perfectly solve this issue (and it should also reduce the complexity of integrating with The only thing I'm wondering/worrying about is the order of the attributes... |
Previously, ```rust async fn foo(_a: u32) {} ``` would compile to ```rust fn foo() {/* something */} ``` This patches changes this behaviour to ```rust fn foo(_a: u32) {/* something */} ``` by simply forwarding the input arguments. This allows any test fixture library (e.g. `rstest`, cfr. la10736/rstest#85) to integrate with actix::test.
I think that your approach is good should be elaborated a bit. We can assume that custom async test attribue should end by test (last PathSegment is Now in every async test we can search in attributes for the last Your example become: #[rstest]
#[actix_rt::test]
async fn foo(logger: Logger) {
} and works also for parametrize and matrix. In this way we can define a default What do you think? |
In this way you don't need to change |
Maybe it's an option to just require some test attribute supplied by the user, and not generate it in FYI: It seems like On the other hand: if the async runtime implements simple argument forwarding (such as in my actix patch) and fixture libraries don't impose a test attribute, it should allow any ordering of the Another option would be to separate the This approach would, I think, require quite the refactoring in the code. Either way, let me know which option you prefer, and I'll implement it today. |
FYI, actix already merged my patch, which should ease future integration with your and other fixture systems. I've also started the same discussion on the Tokio repository, where the fix should be even simpler. I hope you're okay with how this is progressing! |
Your patch can never works with parametrize and matrix. Parametrize and matrix create a single test function for every case and should be annotated by the right attribute but when What I'm trying to explain (and maybe I'm not good enough to get it clear) is that your idea is good but I'm trying to simplify the syntax and unfortunately we should/must relay on attribute ordering because
This is exactly what I've proposed, I used the convection to get I already tested it yesterday and follow code works #[rstest(a, b, case (1, 2), case(3, 4))]
#[actix_rt::test]
async fn foo(a: u32, b: u32) {} This syntax is simple and neat and give a way to use Now I'm thinking to use the injected capability enabled for every kind of tests (async or not). And just switch the default attribute for the async case. Later I'll try to find a way to make configurable the async test attribute. |
Ah right, I get it now, we indeed need to rely on attribute ordering because you cannot copy attributes that aren't there. Thank you for explaining! I hadn't used matrix/parametrized fixture-tests before, that's why I glanced over it.
ack
Are you still referring to matrix/parametrize here (i.e., applying
It's not exactly what I meant by an
Cool! Do you have a topic branch that I can have a look at already? One thing that I wonder about, is for things beyond tests, were you would inject fixtures. I was thinking about benchmarks, for example. I suppose that's for future work, nothing really urgent.
Yes, I like that. It was confusing at first, for me, that |
I'll commit it on master branch later. After I'll work on unit/end2end tests.
Tests are more about ergonomics and less code to write than logic and coherence. So in name of this I'm looking for simple and concise syntax that works in almost all cases. Moreover I'm strongly influenced by |
I've open #91 to implement the proposed behavior and leave this ticket open to find a way of how can the user indicate its default async test attribute instead of |
One idea might be to create features "async-std", "tokio", "actix", and check which is active when the test attribute is generated? It's quite some manual labour/repetitive code, but it will be like that anyhow, I think. |
Yes, that's also my idea of how we can implement it for each runtime. But here I'm looking for a way where a user can define a default test attribute (async or not) without change rstest source code. For instance env variable can works but I'm looking if there is a better way where the user can express in Cargo.toml. |
I've been toying a lot with env and Cargo.toml related variable sourcing, and in my experience it's just a liability. Sourcing from Cargo.toml from the main project requires a |
I'll use env variable to handle this and |
Hey everyone 👋 What's the current state on this? |
Today you can use a custom rest runner as described in https://docs.rs/rstest/latest/rstest/attr.rstest.html#inject-test-attribute This ticket is open just because I would like to fine a good way to define the custom default runner for both sync and async cases. |
1 similar comment
Today you can use a custom rest runner as described in https://docs.rs/rstest/latest/rstest/attr.rstest.html#inject-test-attribute This ticket is open just because I would like to fine a good way to define the custom default runner for both sync and async cases. |
A Cargo.toml table like |
Find a way where the user can choose a custom attribute to mark async tests.
Otherwise they can provide a function or macro to to wrap the calls to async test functions
The text was updated successfully, but these errors were encountered: