Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; MenuItem has a deprecated constructor in /home/charl422/nunitsoftware.com/nunitv2/php/build_menu.php on line 34
NUnit - Exception
NUnit 2.2.4 Legacy Documentation. View NUnit 3 Documentation

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()
    { /* ... */ }
  }
}