Legacy Documentation. View NUnit 3 Documentation

ExpectedExceptionAttribute (NUnit 2.0)

This is the way to specify that the execution of a test will throw an exception. This attribute takes a parameter which is a Type. The runner will execute the test and if it throws the specific exception, then the test passes. If it throws a different exception 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 ExpectAnException()
    { /* ... */ }
  }
}