ExpectedExceptionAttribute (NUnit 2.0 / 2.2.4)

This is the way to specify that the execution of a test will throw an exception. This attribute has two constructors. The first (NUnit 2.0) takes a Type representing the exact type of the expected exception. The second (NUnit 2.2.4) takes the full name of the expected exception type as a string.

In either case, the runner will execute the test, which passes only if it throws the specified exception. If it throws a different exception then the test will fail. This is true even if the thrown exception inherits from the expected exception.

Example:

namespace NUnit.Tests
{
  using System;
  using NUnit.Framework;

  [TestFixture]
  public class SuccessTests
  {
    [Test]
    [ExpectedException(typeof(InvalidOperationException))]
    public void ExpectAnExceptionByType()
    { /* ... */ }

    [Test]
    [ExpectedException("System.InvalidOperationException")]
    public void ExpectAnExceptionByName()
    { /* ... */ }
  }
}