Asserting Exceptions

Introduction

Throwing exceptions is a way for a method to tell its caller that it has operated outside normal operating parameters. Normal and exceptional operations are defined by the method and its API documentation. Application code consists of code that runs without raising exceptions as well as code that raises exceptions during execution. Ideally, test cases for both should be written.

Normal and exception test case development

Normal and exception test cases are both written using asserts. When writing test cases, it is convenient to first define normal test cases followed by exception test cases.

Normal test cases are test cases that test for behavior that does not throw exceptions.

Exception test cases are test cases that test for scenarios where exceptions are thrown.

NUnit exception asserts

The following syntax is used to test for exceptions:

Get hands-on with 1400+ tech skills courses.