none
Exceptions in unit test cases RRS feed

  • Question

  • Hi

    How to write unit test cases along with exceptions using c# in visual studio 2013?

    What are the different types of exceptions?

    Can u please give example except division case

    Thanks & Regards

    Gousiya


    • Edited by Gousiya Wednesday, May 21, 2014 11:31 AM
    Wednesday, May 21, 2014 11:30 AM

All replies

  • What do you mean by along with exceptions? See this for general information on unit tests: Verifying Code by Using Unit Tests.

    Do you want to throw exception when a comparison fails? You should use Assert class for this.

    Exception List link has list of exceptions. Note that this is not complete list.


    TejasJ.

    Wednesday, May 21, 2014 11:46 AM
  • Here is the example that you wanted for unit tests and handling exceptions: http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.expectedexceptionattribute.aspx

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Wednesday, May 21, 2014 12:19 PM
  • You unit test a method for an exception based on the expected exception to be thrown. If the excpetion is not thrown during the unit test, then it's an Assert.Fail with message indiccating that the ecpected exception was not thrown.

    If the expected exception is thrown, the test passes.

    http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.expectedexceptionattribute.aspx

    Wednesday, May 21, 2014 1:01 PM
  • I would personally recommend against using ExpectedExceptionAttribute.  While it will work it can cause false positives.  The issue is that if there is an unhandled exception in the test method and it matches the expected exception then the test passes.  But, in general, all you really want to do is verify a particular action (the part under test) threw the exception.  If any other part of the test method (say the setup logic) throws the exception then the test passes anyway.

    I use FluentAssertions myself which allows you to assert that an action throws a particular exception:

    Action action = () => CodeThatYouExpectToThrow;
    action.ShouldThrow<MyException();

    But implementing this kind of logic isn't that hard if you don't want to rely on third party libraries.  This is a case where declarative isn't better than implicit in my opinion.  More importantly you can expand on the assertion conditions if needed.  For example you could expand the above code to verify that a particular message is generated for a given input.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Wednesday, May 21, 2014 1:54 PM
    Moderator
  • I would personally recommend against using ExpectedExceptionAttribute.  While it will work it can cause false positives.  The issue is that if there is an unhandled exception in the test method and it matches the expected exception then the test passes.  But, in general, all you really want to do is verify a particular action (the part under test) threw the exception.  If any other part of the test method (say the setup logic) throws the exception then the test passes anyway.

    I have to disagree with this. One should know what expected execptions are to be thrown and one tests for that expection to be thrown. If that expection is not thrown, then the test fails.

    There should be a test for each expected expectrion thrown. It's up to the developer to kniow what he or she is testing for and if they don't know, then maybe he or she should  not be writting unit tests.

    Wednesday, May 21, 2014 2:05 PM
  • I agree you should know what exceptions are going to be thrown, but that is not what I said.  I said I don't agree with the attribute because it gives false positives.  And I'm not alone, pretty much anybody using nUnit follows this line of thinking and even the MSTest community is starting to follow this belief.

    Any call that throws the expected exception will cause the test to pass even if it wasn't the method you were actually testing.  In the simplest test case you might have a single method call but as you start testing services that have dependencies and you introduce mocking then the test will evolve to the AAA format.  The Action is the part where you're confirming a particular exception.  But if any other part of the test throws the exception then you get a false positive.  While you would generally catch this when you wrote the initial test, as the code under test evolves you are unlikely to review existing tests that pass looking for false positives.  This has been my experience.

    Wednesday, May 21, 2014 2:36 PM
    Moderator
  • I said I don't agree with the attribute because it gives false positives.  And I'm not alone, pretty much anybody using nUnit follows this line of thinking and even the MSTest community is starting to follow this belief.

    I have never seen a false positive being done on this. When I am unit testing for expected excptions to be thrown, it is based on business rule validation exceptions being thrown out of the business layer where I am testing for business rule validation exceptions, a special exception is being thrown that is being tested.  Bussiness logic in the Bussiness Layer is where I am testing for expected exceptions to be thrown. And the excption is not thrown, then there is someting wrong with the business logic.

    Any other type of unexpected expectations being thrown should make the test fail if they are ever thrown.

    Wednesday, May 21, 2014 3:12 PM
  • Hi,Gousiya

    Do you solve your problem successfully?


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, May 30, 2014 7:52 AM
    Moderator