2011-11-08 47 views
2

我有一個長時間運行的任務方法,使用睡眠異步CTP V3,MS測試和Thread.sleep代碼

public Task LongRunning() { 
    return Task.Factory.StartNew(
     () => { 
      Trace.TraceInformation("Start Sleep"); 

      Thread.Sleep(10000); 

      Trace.TraceInformation("End Sleep"); 
     }); 
} 

這是我的測試調用,它工作正常

[TestMethod] 
public void SimpleContinueWith() { 
    Trace.TraceInformation("Start"); 

    LongRunning() 
     .ContinueWith(
      t => Trace.TraceInformation("End") 
     ).Wait(); 
} 

> QTAgent32.exe Information: 0 : Start 
> QTAgent32.exe Information: 0 : Start Sleep 
> QTAgent32.exe Information: 0 : End Sleep 
> QTAgent32.exe Information: 0 : End 

但使用異步/等待測試直接通過

[TestMethod] 
public async void SimpleAwait() { 
    Trace.TraceInformation("Start"); 

    await LongRunning(); 

    Trace.TraceInformation("End"); 
} 

> QTAgent32.exe Information: 0 : Start 
> QTAgent32.exe Information: 0 : Start Sleep 

爲什麼呢?

回答

4

MSTest不能(當前)處理異步測試。我不確定微軟是否會在最終版本中添加這個功能。 更新:VS11測試版增加了對異步單元測試的支持;見下文。

您可以通過自己提供異步上下文來單元測試異步方法。有一些包含在Async CTP中(Microsoft Visual Studio異步CTP \ Samples \(C#測試)單元測試\ AsyncTestUtilities),或者您可以使用我編寫的名爲AsyncContext的一個。

使用AsyncContext,您的測試可以寫爲:

[TestMethod] 
public void SimpleAwait() { 
    AsyncContext.Run(async() => 
    { 
    Trace.TraceInformation("Start"); 

    await LongRunning(); 

    Trace.TraceInformation("End"); 
    }); 
} 

更新,2012-02-05:另一種選擇是新AsyncUnitTests library。安裝NuGet包,改變你的TestClassAsyncTestClass,你的異步單元測試可以更自然地寫着:

[TestMethod] 
public async void SimpleAwait() { 
    Trace.TraceInformation("Start"); 

    await LongRunning(); 

    Trace.TraceInformation("End"); 
} 

更新,2012-06-06:如果更新到VS2012測試版,您可以定義異步單元測試;他們只需要返回Task

[TestMethod] 
public async Task SimpleAwait() { 
    Trace.TraceInformation("Start"); 

    await LongRunning(); 

    Trace.TraceInformation("End"); 
} 
+0

超,謝謝,安裝NuGet包http://nuget.org/List/Packages/Nito.AsyncEx –

+0

順便說一句,我在寫測試的原因是因爲我有一個調用Parallel.ForEach和遞歸的Async方法 - 它不能與async/await一起工作,就像上面的測試行爲一樣,即被調用並永不回來。 AsyncContext解決了這個問題 - 再次感謝 –