Dot-Net

Assert.Inconclusive 和 IgnoreAttribute

  • January 19, 2011

在 MS 單元測試框架中使用Assert.Inconclusive和使用的正確方法是什麼?IgnoreAttribute

我們Assert.Inconclusive主要用於以下測試:

  • 尚未實施
  • 不知何故損壞或不完整=需要進一步關注
  • 當測試主體因任何原因被註釋掉時

我們這樣做是因為:

  • 不確定的測試可以有消息
  • 我們希望在 TFS 的測試結果中看到這樣的測試

我們的問題是Inconclusive測試在 TFS 和 Resharper 中都被標記為錯誤。如果我們IgnoreAttribute改用,我們將在 Resharper 中看到這些測試,但 MS Test runner 和 TFS 將完全忽略它們。在 TFS 和 MS 測試執行器中使用IgnoreAttribute就像評論整個測試一樣,這是無用的。

我喜歡認為您如何描述 Inconclusive 是正確的用法。

儘管根據我的經驗,Inconclusive 更像是警告而不是錯誤。事實上,它們在 TRX 中與錯誤分開報告:

<TestRun>
  <ResultSummary outcome="Inconclusive">
     <Counters total="1" 
         executed="0" error="0" failed="0" 
         timeout="0" aborted="0" inconclusive="1" 
         passedButRunAborted="0" notRunnable="0" 
         notExecuted="0" disconnected="0" 
         warning="0" passed="0" completed="0" 
         inProgress="0" pending="0" />

我通常從我的 msbuild 腳本中的 <Exec> 任務執行 mstest 執行檔,然後查看 TRX 以確定它是否應該使建構失敗。

我還看到目前實施中的困境。

  • Inconclusive斷言包含在 TRX 報告中,但 mstest.exe(以及 vstest.console.exe)將在執行後返回1(表示錯誤)。
  • 具有該屬性的 TestMethodsIgnore不會被報告為錯誤,但它們在 TRX 報告中完全隱藏

我個人的理解如下:

使用該[Ignore]屬性(暫時)禁用/跳過該方法:

[TestMethod]
[Ignore] // &lt;== disabled through "Ignore" attribute
public void Test001()
{
  //execute some stuff ...
  Assert.IsTrue(...);

  //execute some stuff ...
  Assert.AreEqual(...);
}

不要為此目的濫用Inconclusive斷言:

[TestMethod]
public void Test002()
{
   Assert.Inconclusive(); // &lt;== misuse of "Inconclusive" to skip this test

   //execute some stuff ...
}

相反,Inconclusive應該有條件地使用:僅當我們無法判斷要測試的組件是否按預期工作時。

例如,如果我們依賴的外部資源在測試執行時不可用:

[TestMethod]
public void Test003()
{
   //check if the server is running,
   //otherwise can can't test our local client component!
   if (!WebServiceAvailable())
   {
       Assert.Inconclusive(); // &lt;== skip remaining code because the resource is not available
   }

   //execute some stuff ...
   Assert.AreEqual(...);

   //execute some stuff ...
   Assert.AreEqual(...);
}

_ _

結論:

禁用/跳過測試的邏輯方法是使用[Ignore]屬性。

我清楚地看到目前mstest.exe不報告任何被忽略的測試的行為是一個應該修復的錯誤。

隨意對以下錯誤報告進行投票:

  • <https://connect.microsoft.com/VisualStudio/feedback/details/779743/>
  • <http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2088845>

引用自:https://stackoverflow.com/questions/4733592