C# Unit Tests with Async Task Signature
When writing unit tests, it’s good to standardize as much as possible.
This includes the method signature for each test cases, such as this:
// Test_1_1_1 Describe the test...
[TestMethod]
public async Task Test_1_1_1()
{
// Do some testing...
...
}
The above test case has a return of async Task.
This allows for the test case to execute both async and non-async code.
However, the compiler will throw a warning for any method that is marked async, but contains only synchronous code.
So, to workaround this, while keeping our standard, we need to suppress the warning.
And, the simplest way to do so, is to add a warning suppress directive to the csproj of every test project.
To do this, add the following to the main property group of every test project, like this:
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<NoWarn>1998</NoWarn>
...
No Comments