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()
{ /* ... */ }
}
}
Imports System
Imports Nunit.Framework
Namespace Nunit.Tests
<TestFixture()> Public Class SuccessTests
<Test(), ExpectedException(GetType(Exception))>
Public Sub ExpectAnException()
' ...
End Sub
End Class
End Namespace
#using <Nunit.Framework.dll>
using namespace System;
using namespace NUnit::Framework;
namespace NUnitTests
{
[TestFixture]
public __gc class SuccessTests
{
[Test]
[ExpectedException(__typeof(InvalidOperationException))]
void ExpectAnException();
};
}
#include "cppsample.h"
namespace NUnitTests {
// ...
}