BlackWaspTM
Testing
.NET 1.1+

NUnit Exception Assertions

The tenth part of the Automated Unit Testing tutorial continues looking at the NUnit framework's assertions. This article describes the assert methods that are used to test that the correct exceptions are thrown by the code under test.

Exception Asserts

When you are writing automated unit tests for your code, it can be easy to concentrate on testing the correct pathways through methods, overlooking the situations where exceptions may be thrown. It is important to test incorrect calls as well as correct ones, particularly when writing classes that will be utilised by other programmers, such as when developing code libraries.

In this article we will examine the exception asserts provided by NUnit. These assertion methods ensure that the code under test throws the correct type of exception, or does not throw an exception at all. To demonstrate the asserts we will write tests for a class that calculates the tax for a sales order. The TaxCalculator class and the supporting TaxRate class are shown below:

public class TaxCalculator
{
    public decimal AddTax(decimal orderValue, TaxRate taxRate)
    {
        if (taxRate == null) throw new ArgumentNullException("taxRate");
            return orderValue * (1 + (taxRate.Percentage / 100));
    }
}

public class TaxRate
{
    public string Name { get; set; }
    public decimal Percentage { get; set; }
}

We'll use a single test fixture for testing the TaxCalculator class. Below is the fixture, which includes a set up method and a sample test for the correct execution of the AddTax method.

[TestFixture]
public class TaxCalculatorTests
{
    TaxCalculator _calculator;
    TaxRate _taxRate;
    int _orderValue;

    [SetUp]
    public void SetUp()
    {
        _calculator = new TaxCalculator();
        _taxRate = new TaxRate();
        _taxRate.Name = "VAT";
        _taxRate.Percentage = 20;
        _orderValue = 100;
    }

    [Test]
    public void TaxRateIsAddedCorrectly()
    {
        Assert.AreEqual(120, _calculator.AddTax(_orderValue, _taxRate));
    }
}

Assert.Throws

The first of the exception assertions is Assert.Throws. This allows you to specify the type of exception that you expect some code being tested to throw. In its most basic form, the type of exception is provided as the first parameter. The second parameter defines the code that is to be executed, which should throw the exception. This code is provided using a delegate that matches the TestDelegate type defined by NUnit. The target method must have no parameters and return void.

For our sample test we will use a delegate that points to the following method. This calls AddTax with a null TaxRate.

void CalculateWithNullTaxRate()
{
    _calculator.AddTax(_orderValue, null);
}

We can now create the TestDelegate and use it to check that calling the AddTax method with a null TaxRate throws an ArgumentNullException, using the test below. Note that the first argument for the Assert.Throws method provides the exception type and the second argument is the delegate.

[Test]
public void PassingANullTaxRateCausesAnException()
{
    TestDelegate calculate = new TestDelegate(CalculateWithNullTaxRate);
    Assert.Throws(typeof(ArgumentNullException), calculate);
}

Using Anonymous Methods

Using the delegate style described above is inelegant but required if you are using .NET 1.1. For .NET 2.0 projects, you can use an anonymous method instead of creating the delegate separately, as in the following equivalent example:

[Test]
public void PassingANullTaxRateCausesAnException()
{
    Assert.Throws(typeof(ArgumentNullException),
        delegate() { _calculator.AddTax(_orderValue, null); });
}
22 April 2011