There was a developer came to me, asking me how to perform unit test that check for exception. If the expected exception is thrown, it should consider the test as a success.
We can use ExpectedException attribute to assert exception.
So, here are the sample code:
We can use ExpectedException attribute to assert exception.
So, here are the sample code:
[TestMethod(), ExpectedException(typeof(ApplicationException),
"Something is wrong, no exception had been thrown.")]
public void TestMethod1()
{
//write some code here which will throw ApplicationException
throw new ApplicationException("Test
assert exception");
}
No comments:
Post a Comment