I am writing unit tests on each of several methods MyMethod1
, MyMethod2
, ...
For each method, I would like to test a case when it raises a particular exception and a case when it doesn't.
What I do now is to group the two cases for each method into a test method:
[TestMethod]
[TestCategory("Unit")]
public void MyTest()
{
SetupExceptionCase();
try
{
MyMethod1();
Assert.Fail();
}
catch (InvalidDataException) { }
SetupNonExceptionCase();
try
{
MyMethod1();
}
catch (InvalidDataException)
{
Assert.Fail();
}
}
If I would like to use ExpectedException
attribute to replace the more lengthy try...catch...
control, as suggested in https://stackoverflow.com/a/933627/156458,
do I need to split my test method into two test methods, each for a case?
If yes, how can I use the attribute for the case where I don't expect my method to raise an exception?
Is there any way that I don't have to split it, because grouping two cases for the same method to be tested in a test method separates nicely the tests for different methods to be tested?
Thanks.