The main benefits of async code are a flexible client-side user interface and server-side scalability. For unit tests, you get a little scalability (which is consistent with the overall speed benefits, since unit tests are torn by nature).
But this is not a huge benefit. Your tests (probably) will run faster.
I usually use async Task unit test methods for the following reasons:
- If you are testing code inside the context, blocking can cause a classic deadlock problem. Please note that some frameworks (e.g. xUnit) always provide a default context. Even for other frameworks, it is often necessary to provide context for unit test ViewModels.
await does not wrap exceptions in an AggregateException .- You get a little scalability advantage, which (theoretically) allows your unit tests to run faster overall. Assuming your infrastructure runs your tests in parallel.
- Why not? They are as simple as synchronous methods.
async Task unit test methods are supported by all major unit test structures since 2012.
source share