Test with NO expected exception

C#.NetUnit TestingTestingNunit

C# Problem Overview


I want to create NUnit test to ensure that my function does not throw an exception. Is there some specific way to do it, or I should just write

[Test]
public void noExceptionTest() {
  testedFunction();
}

and it will succeed if no exception is thrown?

C# Solutions


Solution 1 - C#

Assert.DoesNotThrow(() => { /* custom code block here*/});

OR just method

Assert.DoesNotThrow(() => CallMymethod());

For more details see NUnit Exception Asserts

Solution 2 - C#

Using NUnit 3.0 Constraint Model type assertions the code would look as follows:

Assert.That(() => SomeMethod(actual), Throws.Nothing);

This example is taken from NUnit wiki.

Solution 3 - C#

Not throwing an exception is the normal course of action. Your test will successfully verify that an exception is not thrown.

Solution 4 - C#

You are correct. If there's an exception then the test will fail.

Unless you specify

[ExpectedException( typeof(YourException) ) ]
public void TestMethod()
{ 
   ...
}

Or as @sll says, you use the more specific

Assert.DoesNotThrow

assertion.

Solution 5 - C#

Yes, no thrown exceptions -> test pass. If there was try-catch block without re-throw, it'll pass too.

Solution 6 - C#

I think there is a problem related to unit test logic. If you are expecting a specific exception under certain inputs, you declare it as an expected exception. If you are just checking whether your function behaves properly and no exceptions are expected during this proper behavior, you just write it and if it throws any exception, your test fails.

Your code seems to be working properly, on the other hand, only checking no exceptions may not be the proper way for unit testing. In unit tests, generally you expect some outputs (expected values), you have some actual outputs (actual values) and you assert that expected and actual values are the same.

Solution 7 - C#

If you want to stick to the Arrange-Act-Assert pattern, you can define the TestDelegate beforehand and assert it later on. The function is still executed in the Assert part, but the structure is clearer.

// Arrange
var dataHandler = this.GetTestDataHandler();

// Act
TestDelegate transformAction = () => dataHandler.TransformData();

// Assert
Assert.DoesNotThrow(transformAction);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYury PogrebnyakView Question on Stackoverflow
Solution 1 - C#sllView Answer on Stackoverflow
Solution 2 - C#Jack UklejaView Answer on Stackoverflow
Solution 3 - C#Sergey KalinichenkoView Answer on Stackoverflow
Solution 4 - C#PhilView Answer on Stackoverflow
Solution 5 - C#AntonView Answer on Stackoverflow
Solution 6 - C#daryalView Answer on Stackoverflow
Solution 7 - C#Sebastian BittisView Answer on Stackoverflow