2017-07-27 21 views
0

[重複的] Assert in Try..Catch block is caughtxUnit的核心是不工作的權利

當我使用的xUnit核心在NetCore1.1在VS2017爲folllow:

enter image description here

 try 
     { 
      Assert.True(1<0," server is false");//vs2017 show green(test through) 

     } 
     catch (Exception e) 
     { 
      while (e != null) 
      { 
       _outputHelper.WriteLine(e.Message); 
       e = e.InnerException; 
      } 
     } 

     Assert.True(1 < 0, "current value is false");// vs2017 show red(test failed 

結果圖標是綠色的,不是紅色的時候第一個Assert.True不對(失敗)。如下圖:

enter image description here

如果加上第二個Assert.True它是紅色的;

enter image description here

這是非常奇怪的,是嗎?我認爲這是一個錯誤。

回答

3

這不是一個錯誤。

所有的單元測試框架,包括xUnit.net,都使用異常來判斷失敗。你一味地抓住併吞下所有例外;這將包括失敗斷言的例外。

這也是不清楚你要用這段代碼來完成什麼。也許這是爲了說明的目的而進行的簡化......?

+0

Hi @Brad Wilson,謝謝你的回覆?我想測試EF演示功能,[這裏](https://github.com/huoxudong125/HQF.Tutorials.EntityFrameworkCore/blob/master/Tests/HQF.Tutorials.EntityFrameworkCore.XUnitTest/UnitTest1.cs)是完整的演示碼。有沒有一個好的模式來實現? – huoxudong125

+0

只是不抓。任何拋出的異常都將顯示爲失敗。你可以離開try/finally進行資源清理,只是沒有捕獲。 –