87

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?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Yury Pogrebnyak
  • 4,093
  • 9
  • 45
  • 78
  • 2
    Is that really all you want to test? You do realise that if testedFunction is a no-op it will pass the test. – Martijn Mar 21 '12 at 11:38
  • Does your method have a particular requirement not to throw? Or is it done in the spirit of *my code works because it doesn't throw*? If the later one, you migth want to check [those](http://stackoverflow.com/questions/8788558/should-i-test-that-methods-dont-throw-exceptions) [two](http://stackoverflow.com/questions/9417213/c-how-do-i-check-no-exception-occurred-in-my-unit-test) questions why in general you don't do that. You gain very little to nothing from such test. – k.m Mar 21 '12 at 13:36
  • 1
    @jimmy_keen As an exceptional case, have used to test that calling Dispose() a second time does not throw – Gareth Nov 12 '14 at 13:59
  • @Gareth: why was it allowed to throw during first invocation and not during second? – k.m Nov 12 '14 at 15:40
  • 1
    @jimmy_keen think you've misunderstood, it doesn't throw during the first invocation - the test is to check that the second call doesn't cause an exception either. "If an object's Dispose method is called more than once, the object must ignore all calls after the first one. The object must not throw an exception if its Dispose method is called multiple times. " http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx – Gareth Nov 12 '14 at 15:57

7 Answers7

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

OR just method

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

For more details see NUnit Exception Asserts

sll
  • 61,540
  • 22
  • 104
  • 156
43

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.

Jack Ukleja
  • 13,061
  • 11
  • 72
  • 113
  • I get ambiguous invocation and I'm unsure how to solve it. Version I'm using is the one shipped with Unity 2018.4 and should be above 3.5. – jeromej Sep 30 '19 at 08:00
7

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

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 5
    I agree, but I think using Assert.DoesNotThrow makes it clear that it's an actual test vs a test that's just used to execute code. – reggaeguitar Apr 02 '19 at 17:42
3

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.

Phil
  • 42,255
  • 9
  • 100
  • 100
1

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);
Sebastian
  • 11
  • 2
1

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

Anton
  • 1,409
  • 1
  • 19
  • 37
1

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.

daryal
  • 14,643
  • 4
  • 38
  • 54