Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check "no exception occurred" in my MSTest unit test?

I'm writing a unit test for this one method which returns "void". I would like to have one case that the test passes when there is no exception thrown. How do I write that in C#?

Assert.IsTrue(????) 

(My guess is this is how I should check, but what goes into "???")

I hope my question is clear enough.

like image 695
CuriousGeorge Avatar asked Feb 23 '12 16:02

CuriousGeorge


People also ask

How do I run a MSTest unit test?

To run MSTest unit tests, specify the full path to the MSTest executable (mstest.exe) in the Unit Testing Options dialog. To call this dialog directly from the editor, right-click somewhere in the editor and then click Options.

How do you assert exception is not thrown in C#?

In NUnit, you can use: Assert. DoesNotThrow(<expression>); to assert that your code does not throw an exception.


2 Answers

Your unit test will fail anyway if an exception is thrown - you don't need to put in a special assert.

This is one of the few scenarios where you will see unit tests with no assertions at all - the test will implicitly fail if an exception is raised.

However, if you really did want to write an assertion for this - perhaps to be able to catch the exception and report "expected no exception but got this...", you can do this:

[Test] public void TestNoExceptionIsThrownByMethodUnderTest() {     var myObject = new MyObject();      try     {         myObject.MethodUnderTest();     }     catch (Exception ex)     {         Assert.Fail("Expected no exception, but got: " + ex.Message);     } } 

(the above is an example for NUnit, but the same holds true for MSTest)

like image 59
Rob Levine Avatar answered Sep 18 '22 13:09

Rob Levine


In NUnit, you can use:

Assert.DoesNotThrow(<expression>);  

to assert that your code does not throw an exception. Although the test would fail if an exception is thrown even if there was no Assert around it, the value of this approach is that you can then distinguish between unmet expectations and bugs in your tests, and you have the option of adding a custom message that will be displayed in your test output. A well-worded test output can help you locate errors in your code that have caused a test to fail.

I think it's valid to add tests to ensure that your code is not throwing exceptions; for example, imagine you are validating input and need to convert an incoming string to a long. There may be occasions when the string is null, and this is acceptable, so you want to ensure that the string conversion does not throw an exception. There will therefore be code to handle this occasion, and if you haven't written a test for it you will be missing coverage around an important piece of logic.

like image 40
Clarkeye Avatar answered Sep 19 '22 13:09

Clarkeye