UnitTest ExpectedException with multiple Exceptions

Posted by masterchris_99 on Stack Overflow See other posts from Stack Overflow or by masterchris_99
Published on 2011-11-23T07:55:19Z Indexed on 2011/11/23 9:50 UTC
Read the original article Hit count: 305

Filed under:
|
|

I want one TestMethod for multiple exceptions. The Problem ist that the Testmethod stops after the first thrown exception.

I know that I can do something like that:

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

But I want to use the following code-base:

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

And I don't want to create one testmethod for each possible exception like that:

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

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about unit-testing