Secondo this discussion, non ci dovrebbe essere alcuna differenza tra i due metodi seguenti:Perché i test di unità asincrone falliscono quando le parole chiave asincrone/attese non vengono utilizzate?
public async Task Foo()
{
await DoSomethingAsync();
}
public Task Foo()
{
return DoSomethingAsync();
}
In realtà, sembrerebbe che per i metodi molto semplici, l'invocazione senza Async/attendono parole chiave sarebbe preferibile, come rimuovono alcuni overhead.
Tuttavia, questo a quanto pare non funziona sempre nei test di unità.
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 tutti i casi, passa
Test1
. - In MSTest,
Test2
rivela nel test runner, ma non viene eseguito. In NUnit,
Test2
viene ignorato, con il messaggio:Metodo di prova ha tipo di ritorno non vuoto, ma nessun risultato è atteso
In xUnit,
Test2
passaggi.
Dal momento che i compiti sono ancora awaitable in ogni caso, che cosa si tratta la parola async
che colpisce i corridori di prova NUnit e MSTest? Forse qualche problema di riflessione?
In realtà, c'è [una certa differenza] (http://stackoverflow.com/a/21082631/1768303) tra 'Test1' e' Test2' :) – Noseratio
@Noseratio - Grazie! –