2011-11-23 31 views
5

我想要一個TestMethod來處理多個異常。問題在於Testmethod在第一次拋出的異常之後停止。有多個異常的UnitTest ExpectedException

我知道我可以做這樣的事情:

try 
{ 
    sAbc.ToInteger(); 
    Assert.Fail(); // If it gets to this line, no exception was thrown 
} 
catch (ArgumentException) { } 

但我想使用下面的代碼基:

[TestMethod, ExpectedException(typeof(ArgumentException), "...")] 
public void StringToIntException() 
{ 
    sAbc.ToInteger(); // throws an exception and stops here 
    sDecimal.ToInteger(); // throws theoretically a exception too... 
} 

而且我不希望創建一個TestMethod的每個可能的例外如下:

[TestMethod, ExpectedException(typeof(ArgumentException), "...")] 
public void StringToIntException() 
{ 
    sAbc.ToInteger(); 
} 

[TestMethod, ExpectedException(typeof(ArgumentException), "...")] 
public void StringToIntException() 
{ 
    sDecimal.ToInteger(); 
} 

回答

0

據我所知,這是不可能的屬性,因爲當你exce ption被拋出,方法的執行被中斷。因此,如果您在第一行有異常,則不會執行第二行。

如果你真正需要的功能,使用NUnit其中有:

Assert.Throws<Exception>(delegate { /*Your code*/ }); 
+0

怎麼樣或類型多個異常?一次只會發生一個例外情況。這可以通過mstest實現嗎? – liang