2013-08-22 64 views
2

我想忽略某些測試,這些測試是基於我在TestFixtureSetUp期間從配置文件中提取的數據。有沒有辦法忽略基於參數運行測試?選擇性忽略NUnit測試

[TestFixture] 
public class MessagesTests 
{ 
    private bool isPaidAccount; 

    [TestFixtureSetUp] 
    public void Init() { 
     isPaidAccount = ConfigurationManager.AppSettings["IsPaidAccount"] == "True"; 
    } 

    [Test] 
    //this test should run only if `isPaidAccount` is true 
    public void Message_Without_Template_Is_Sent() 
    { 
     //this tests an actual web api call. 
    } 

} 

如果我們測試的帳戶是付費帳戶,測試應該正常運行,否則該方法會拋出異常。

是否會有擴展屬性[Ignore(ReallyIgnore = isPaidAccount)]?或者我應該在方法中寫入這些內容,併爲例如2個單獨的測試用例運行。

public void Message_Without_Template_Is_Sent() 
    { 
     if(isPaidAccount) 
     { 
       //test for return value here 
     } 
     else 
     { 
       //test for exception here 
     } 
    } 
+0

好問題。 +1 –

+3

'Assert.Ignore()'在測試中。 – Matthew

+0

@Matthew所以它很好的做法,只是檢查條件之前,忽略測試? –

回答

0

您可以使用Assert.Ignore()類似馬修的狀態。如果您想對結果進行不同的分類,則還可以使用Assert.Inconclusive()

這個問題/答案有點類似:Programmatically skip an nunit test