2011-06-30 43 views
6

我有一組單元測試並行運行,這使得調用Web客戶端下載資源(如果測試運行單線程這是重要的,它工作正常)這會持續超過30秒鐘返回,並且這會導致單元測試以下列兩條消息之一強制退出:VS單元測試 - 線程已被中止

線程正在中止。

線程運行的應用程序域已被卸載。

我已經嘗試設置[Timeout]屬性,各種app.config設置,甚至包括創建一個EventWaitHandle使Web客戶端線程的單元測試線程等待,沒有運氣。我檢查了測試設置下的測試超時設置,並將其設置爲30分鐘的默認值。


編輯2:

如@peer指示的,這是在VS.Net測試框架一個已知的錯誤:http://connect.microsoft.com/VisualStudio/feedback/details/587390/threadabortexception-when-running-two-tests-in-parallel-one-taking-40-seconds

編輯:

這裏是最簡單的情況將會重現問題。你可以讓這些單元測試運行完成,如果是的話,如何? 這些測試必須並行運行!啓動一個新的Visual Studio單元測試項目,並使用以下設置和測試方法代碼。當你運行時,確保它們實際上是並行運行的(也就是說,你將需要一個具有多個內核的處理器,並檢查它們是否同時運行。我發現爲了讓它們並行運行,我必須將所有設置,然後關閉項目並在應用並行之前重新打開它)。

Local.testsettings(添加parallelTestCount="#",無論適用於您的處理器):

<Description>These are default test settings for a local test run.</Description> 
<Deployment enabled="false" /> 
<Execution parallelTestCount="4"> 
    <TestTypeSpecific /> 
    <AgentRule name="Execution Agents"> 
    </AgentRule> 
</Execution> 

TraceAndTestImpact.testsettings(註釋掉DataCollectors):

<Description>These are test settings for Trace and Test Impact.</Description> 
<Execution parallelTestCount="0"> 
    <TestTypeSpecific /> 
    <AgentRule name="Execution Agents"> 
    <!--<DataCollectors> 
     <DataCollector uri="datacollector://microsoft/SystemInfo/1.0" assemblyQualifiedName="Microsoft.VisualStudio.TestTools.DataCollection.SystemInfo.SystemInfoDataCollector, Microsoft.VisualStudio.TestTools.DataCollection.SystemInfo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" friendlyName="System Information"> 
     </DataCollector> 
     <DataCollector uri="datacollector://microsoft/HttpProxy/1.0" assemblyQualifiedName="Microsoft.VisualStudio.TraceCollector.HttpProxyCollector, Microsoft.VisualStudio.TraceCollector, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" friendlyName="ASP.NET Client Proxy for IntelliTrace and Test Impact"> 
     </DataCollector> 
     <DataCollector uri="datacollector://microsoft/TestImpact/1.0" assemblyQualifiedName="Microsoft.VisualStudio.TraceCollector.TestImpactDataCollector, Microsoft.VisualStudio.TraceCollector, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" friendlyName="Test Impact"> 
     </DataCollector> 
     <DataCollector uri="datacollector://microsoft/TraceDebugger/1.0" assemblyQualifiedName="Microsoft.VisualStudio.TraceCollector.TraceDebuggerDataCollector, Microsoft.VisualStudio.TraceCollector, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" friendlyName="IntelliTrace"> 
     </DataCollector> 
    </DataCollectors>--> 
    </AgentRule> 
</Execution> 

的UnitTest1.cs(第一次單元測試,睡35秒):

[TestMethod] 
public void TestMethod1() 
{ 
    Thread.Sleep(35000); 
    // in default unit test settings, this line will never be reached 
    Console.WriteLine("TestMethod1"); 
} 

UnitTest2.cs(第二單元測試,睡35秒):

[TestMethod] 
public void TestMethod2() 
{ 
    Thread.Sleep(35000); 
    // in default unit test settings, this line will never be reached 
    Console.WriteLine("TestMethod2"); 
} 

如果您有並行安裝是否正確,你會發現,這兩個測試將失敗,一個ThreadAbortException,與在頂部顯示的兩條消息中的任一條。我怎麼能告訴這些方法運行超過30秒?

+0

似乎爲我工作... –

+0

我覺得應該做的事情是使用像犀牛或起訂量嘲弄的框架來模擬Web服務。在單元測試中調用Ws並不是一個好習慣 – boca

+0

@MikeGoatly:我發現我錯過了一塊。它適用於我,直到我並行執行,然後我收到錯誤消息。 – mellamokb

回答

3

你應該去菜單:測試 - >編輯測試設置 - >電流測試配置

去標籤:測試超時

更改超時到你喜歡的任何時間。

+0

@peer:目前配置爲30分鐘... – mellamokb

+0

您的測試工作正常。最可能的問題是使用的配置不是您正在編輯的配置。嘗試將超時更改爲1秒,看它是否在1秒後結束。 – Peter

+0

@peer:發現是什麼讓它停止工作。當我單線程運行測試時,它對我來說也很好。當我並行運行多個測試時,我開始再次獲取線程被中止的消息。 – mellamokb

1

也許您的測試設置配置了超時?

enter image description here

+0

它目前配置爲30分鐘... – mellamokb

+0

合理,這是默認設置。 – bryanbcook

+0

發現是什麼讓它停止工作。當我單線程運行測試時,它對我來說也很好。當我並行運行多個測試時,我開始再次獲取線程被中止的消息 – mellamokb

0

我也只是有一個多線程的測試得到了這個。

在任務完成之前,測試正在進行斷言。斷言失敗,測試工具中止任務。

我添加了一個等待循環,直到任務完成,所有工作都像魅力一樣。

克里斯