thomhurst / TUnit

A modern, fast and flexible .NET testing framework
MIT License
1.21k stars 12 forks source link

Unnecessary and prohibiting error about `await using` on `Assert.Multiple()` #586

Open Rekkonnect opened 2 hours ago

Rekkonnect commented 2 hours ago

I'm using the version 0.1.790, which only contains the single Assert.Multiple() overload that can be used as a scope without writing all the assertion logic inside a lambda expression passed into the multiple assertion scope.

There is an analyzer that reports the following error:

Which I find unnecessary. If you don't include the await keyword, the C# compiler itself will report an error about applying a simple using on an IAsyncDisposable, hinting using await using.

Example screenshot:

image

Workaround

To use this you need to suppress the error by doing:

#pragma warning disable TUnitAssertions0004 // Assert.Multiple needs an `await using` before it

VS provides a quick fix for that by suppressing and then restoring the error, although I would recommend completely disabling it throughout the entire file, or in the .editorconfig entirely.

thomhurst commented 2 hours ago

Thanks - I'll look into this. Current workaround (apart from the pragma disable) is using a scope for the using:

await using (Assert.Multiple())
{
    ...
}
Rekkonnect commented 2 hours ago

Imo it's best to completely remove this error, for the reason I explained above. I prefer typing the extra var _ = to allow the inline syntax, and avoid nesting all my assertions within the scope, since most of my testing logic is very small and linear.

thomhurst commented 2 hours ago

I'll just make the analyzer accept both versions. I won't remove it because the dispose call is what performs the assertions, so if there's no analyzer to remind people to dispose then their assertions wouldn't run

Rekkonnect commented 2 hours ago

Oh I completely missed the point of this diagnostic, thanks for the explanation