Im writing a unit test for a c# class, One of my tests should cause the method to throw an exception when the data is added. How can i use my unit test to confirm that the exception has been thrown?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It depends on what unit test framework you’re using. In all cases you could do something like:
In some frameworks there’s an attribute you can add to the test method to express the expected exception, or there may be a method, such as:
It’s nicer to limit the scope like this, as if you apply it to the whole test, the test could pass even if the wrong line threw the exception.