According to this discussion, there should be no difference between the following two methods:
public async Task Foo()
{
await DoSomethingAsync();
}
public Task Foo()
{
return DoSomethingAsync();
}
Actually, it would seem that for very simple methods, the invocation without the async/await keywords would be preferred, as they remove some overhead.
However, this apparently doesn't always work in unit tests.
MSTest
[TestClass]
public class AsyncTest
{
[TestMethod]
public async Task Test1()
{
await Task.Delay(0);
}
[TestMethod]
public Task Test2()
{
return Task.Delay(0);
}
}
NUnit
[TestFixture]
public class AsyncTest
{
[Test]
public async Task Test1()
{
await Task.Delay(0);
}
[Test]
public Task Test2()
{
return Task.Delay(0);
}
}
XUnit
public class AsyncTest
{
[Fact]
public async Task Test1()
{
await Task.Delay(0);
}
[Fact]
public Task Test2()
{
return Task.Delay(0);
}
}
- In all cases,
Test1passes. - In MSTest,
Test2shows up in the test runner, but it doesn't run. In NUnit,
Test2is ignored, with the message:Test method has non-void return type, but no result is expected
In XUnit,
Test2passes.
Since the tasks are still awaitable in all cases, what is it about the async keyword that affects the NUnit and MSTest test runners? Perhaps some reflection issue?