C# > Testing and Debugging > Unit Testing > Writing Test Cases
Testing Exception Handling
This snippet demonstrates how to write a unit test in C# to verify that a method throws the expected exception. This is crucial for ensuring that your code handles error conditions gracefully.
Code Snippet
This code defines a Calculator
class with a Divide
method that throws an ArgumentException
if the denominator is zero. The CalculatorTests
class contains a test method that uses Assert.Throws
to verify that the expected exception is thrown when dividing by zero. The Assert.Throws
method takes a lambda expression that encapsulates the code that should throw the exception.
using NUnit.Framework;
using System;
public class Calculator
{
public int Divide(int numerator, int denominator)
{
if (denominator == 0)
{
throw new ArgumentException("Denominator cannot be zero.");
}
return numerator / denominator;
}
}
[TestFixture]
public class CalculatorTests
{
[Test]
public void Divide_ByZero_ThrowsArgumentException()
{
// Arrange
Calculator calculator = new Calculator();
// Act & Assert
Assert.Throws<ArgumentException>(() => calculator.Divide(10, 0));
}
}
Concepts Behind the Snippet
Testing exception handling is vital for ensuring that your application behaves predictably and reliably in exceptional circumstances. Assert.Throws
is a powerful tool for verifying that methods throw the correct type of exception under specific conditions. This helps prevent unexpected crashes and provides more informative error messages to users or administrators.
Real-Life Use Case
Imagine you're developing an API endpoint that handles user registration. You would want to test that the API correctly throws exceptions when a user tries to register with an existing email address, an invalid password, or missing required fields. These tests ensure that the API behaves consistently and provides meaningful error responses to the client.
Best Practices
Assert.Throws
is designed specifically for testing exceptions.
Interview Tip
Be prepared to discuss different types of exceptions in C#, such as ArgumentException
, NullReferenceException
, and IOException
. Also, be ready to explain how to handle exceptions using try-catch blocks and how to write unit tests for exception handling.
When to Use Them
Use these tests whenever a method is expected to throw an exception under certain conditions. This is particularly important for methods that handle user input, interact with external resources, or perform complex calculations.
Alternatives
While Assert.Throws
is the standard way to test exceptions in NUnit, you could also use a try-catch block within the test method and manually assert the type of exception caught. However, Assert.Throws
provides a more concise and readable way to achieve the same result.
Pros
Cons
FAQ
-
What happens if the code inside `Assert.Throws` doesn't throw an exception?
If the code inside `Assert.Throws` doesn't throw an exception, the test will fail, indicating that the expected exception was not thrown. -
Can I test for a specific exception message using `Assert.Throws`?
Yes, you can useAssert.Throws
with an additional parameter to check the exception message. You can useAssert.That(() => calculator.Divide(10, 0), Throws.ArgumentException.With.Message.EqualTo("Denominator cannot be zero."));
-
How do I test for multiple exceptions in a single test method?
It's generally best to keep tests focused and test for one specific exception per test method. However, if you need to test for multiple exceptions, you can write separateAssert.Throws
statements for each exception.