2011-11-23 30 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