-
Notifications
You must be signed in to change notification settings - Fork 783
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
Expose CancellationToken
as passed-in argument to tests
#3069
Comments
We're going to have to agree to disagree. Adding features like this increases both the complexity of the implementation, and the need for users to understand which method parameters are fulfilled from which sources and why. You could make an argument for passing every value from I argue that it's simpler to let users know about |
Interesting. I would've thought that passing cancellation token via a parameter would be universally seen as the more standard thing to do whenever cancellation is involved in any operation, so I wasn't expecting pushback on that idea. Fair enough though.
Absolutely. Not denying that.
Again, if you consider that every other common framework in the ecosystem can provide a
I would not argue in favor of that, no. I could see maybe passing the whole [Fact]
public async Task MyTestAsync(TestContext testContext, CancellationToken cancellationToken)
{
await myService.DoSomethingAsync(cancellationToken);
} But in that scenario I would still advocate for the
Now you are making an argument against something I did not propose at all. I've not mentioned 17 things, I mentioned just 1, super idiomatic one, that basically every other framework in the entire .NET ecosystem already leverages. On a related note, I checked some of the extension interfaces and was wondering how one could make this work via a custom extension, if so desired. I saw that some of the interfaces that expose test method calling arguments rely on arrays, meaning you cannot "add arguments" to the call in the extension. Is that really the case, or is there an extension point in the framework that would allow one to pass |
You're arguing for your request. I'm arguing against the number of requests that would come after it. "Why is We have never supported anything coming to a method other than theory data. A simple followup question is: What if your theory data includes a Keeping things easier to reason about is better for everybody.
The method arguments are given to the test case, so it's the test case discoverer which is responsible for supplying them (or, in the case of The real problem you have here is that the test context is contextual. The value for the cancellation token at any given point in the pipeline is different, so grabbing it during (for example) test case discovery (where test method arguments are usually determined) would be useless anyways. The only time you could effectively grab it is immediately before invoking the test method. That means the only real override point is by overriding xunit/src/xunit.v3.core/Runners/TestRunner.cs Lines 147 to 185 in 450dd5a
|
The answer is precisely that "yes, People seeing
That's fair, I understand it. You also didn't ever support cancellation, now you do. New things are added to frameworks.
This is a fair question, even though passing a cancellation token via Thanks for the code sample. I'll take a look at that. |
I'm afraid we're at the end of this discussion, because we simply disagree, and neither is convincing the other. |
I've started a migration towards
xunit.v3
and immediately ran into the newxUnit1051
warning about propagating the built-inTestContext.Current.CancellationToken
value to underlying calls for test cancellation.This is all well and good, I like the new capability, but I'd like to propose that the engine also supports propagation of this token as an argument on the test method itself.
I'd like to go from this:
To this:
I strongly believe the latter is much more natural and idiomatic in the .NET ecosystem than fetching the token from an ambient object. This approach also leads to simpler overall code as there is a lot less boilerplate involved.
I would assume such a feature should be straightforward to implement by checking the signature of the method right before performing the call: if it has an argument of type
CancellationToken
, pass theTestContext.Current.CancellationToken
through that value.In case the method also takes in inline data (for
[Theory]
methods), the token could still be provided using a similar strategy that, say, ASP.NET Core uses for binding the token parameter to controller actions: as long as there is one argument of typeCancellationToken
anywhere in the arguments list, it passes the token through that, regardless of parameter order.This argument should of course be ignored in discovery processes (so it wouldn't show up in, say, Visual Studio Test Explorer).
The text was updated successfully, but these errors were encountered: