chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male

Always Assert Something

a unit test is a way to check something in the application. The most common way is to use the “Assert.xx” methods that are found in all common unit testing frameworks.
Without an assert your test is only going through the application’s code but is not really checking that something is true or false.

Possible types of asserts include:

  • [ExpectedException(…)] attribute on top of the test method
  • Assert.Throws(..)
  • Assert.IsTrue, IsFalse etc..
  • Verification against Isolation frameworks (Isolate.Verify, mock.AssertWasCalled, mock.Verify etc.)

What if I’m just testing that no exception is being thrown?

the only time it makes sense to not have an assert in your test is when you are testing that doing a specific action in the application under test does not throw an exception. in that case – your test name should point out that you are checking that no exception is being thrown – otherwise, whoever reads your test will not understand what is the expected behavior of the system under test.
Examples of such test names:
  1. Login_Called_DoesNotThrowException()
  2. Can_Call_Login_WithouException()
  3. LoginShouldNotThrowException()
  4. NoExcpeptionThrownFromLogin()

Just make sure that you mention that an exception is not thrown, for the sake of your test readers.

Comments are closed.