Declares that the associated test's TestOutcome should be interpreted as having completed with a specified TestOutcome if a specified exception is thrown during the test.

Namespace:  MbUnit.Framework
Assembly:  MbUnit (in MbUnit.dll) Version: 3.3.0.0 (3.3.610.0)

Syntax

C#
public class CatchExceptionAttribute : TestDecoratorPatternAttribute
Visual Basic (Declaration)
Public Class CatchExceptionAttribute _
	Inherits TestDecoratorPatternAttribute

Remarks

The expected contents of the detected exception's Message may optionally be specified.

The TestOutcome that is returned when a matching exception is thrown may optionally be specified. By default, the test will be treated as Error if a matching exception is thrown.

Examples

This C# example shows a test with a CatchExceptionAttribute that will result in a Error (the default Outcome) if the test throws a NotSupportedException.

CopyC#
/// <summary>
/// This test will have an outcome of <see cref="TestOutcome.Error"/>.
/// </summary>
[Test]
[CatchException(typeof(NotSupportedException))]
public void CatchExceptionExample1()
{
    throw new NotSupportedException();
}

This C# example shows a test with a CatchExceptionAttribute that will result in a Pending if the test throws a NotImplementedException.

CopyC#
/// <summary>
/// This test will have an outcome of <see cref="TestOutcome.Pending"/>.
/// </summary>
[Test]
[CatchException(typeof(NotImplementedException), StandardOutcome = "TestOutcome.Pending")]
public void CatchExceptionExample2()
{
    throw new NotImplementedException();
}

This C# example shows a test with a CatchExceptionAttribute that will result in a Passed if the test throws a NotImplementedException or if the test doesn't throw any exceptions.

CopyC#
/// <summary>
/// This test will have an outcome of <see cref="TestOutcome.Passed"/> 
/// whether the test throws a <see cref="NotImplementedException"/> or throws no exception.
/// </summary>
[Test]
[CatchException(typeof(NotImplementedException), StandardOutcome = "TestOutcome.Passed")]
public void CatchExceptionExample3()
{
    bool randomBool = new Random().Next() % 2 == 0;
    if (randomBool)
        throw new NotImplementedException();
}

This C# example shows a test with multiple CatchExceptionAttributes that will: result in a TestOutcome of Pending if the test throws a NotImplementedException; result in a TestOutcome with a Status of Skipped and a Category of "notsupported" if the test throws a NotSupportedException; result in a TestOutcome with a Status of Inconclusive and a Category of "deadlock" if the test throws a TimeoutException with a Message that contains the substring "deadlock"; result in a TestOutcome with a Status of Inconclusive and a Category of "timeout" if the test throws a TimeoutException with a Message that contains the substring "server not responding"; result in a TestOutcome of Failed if the test throws any other Exception not listed above; result in a TestOutcome of Passed if the test does not throw any Exceptions.

CopyC#
/// <remarks>
/// This test can have any of several outcomes depending on what type of exception is thrown (if any) during test execution.
/// </remarks>
[Test]
[CatchException(typeof(NotSupportedException), OutcomeStatus = TestStatus.Skipped, OutcomeCategory = "notsupported")]
[CatchException(typeof(NotImplementedException), StandardOutcome = "TestOutcome.Pending")]
[CatchException(typeof(TimeoutException), ExceptionMessage = "deadlock", OutcomeStatus = TestStatus.Inconclusive, OutcomeCategory = "deadlock", Order = 2)]
[CatchException(typeof(TimeoutException), ExceptionMessage = "server not responding", OutcomeStatus = TestStatus.Inconclusive, OutcomeCategory = "timeout", Order = 3)]
public void CatchExceptionExample4()
{
    //throw new NotSupportedException(); //outcome would be: new TestOutcome(TestStatus.Skipped, "notsupported")
    throw new NotImplementedException(); //outcome would be: TestOutcome.Pending
    //throw new TimeoutException("A deadlock occurred."); //outcome would be: new TestOutcome(TestStatus.Inconclusive, "deadlock")
    //throw new TimeoutException("The server is not responding."); //outcome would be: new TestOutcome(TestStatus.Inconclusive, "timeout")
    //throw new TimeoutException("Transaction timeout."); //outcome would be: TestOutcome.Failed
    //throw new TimeoutException(); //outcome would be: TestOutcome.Failed
    //throw new ArithmeticException(); //outcome would be: TestOutcome.Failed
    //return; //outcome would be: TestOutcome.Passed
}

Inheritance Hierarchy

System..::.Object
  System..::.Attribute
    Gallio.Framework.Pattern..::.PatternAttribute
      Gallio.Framework.Pattern..::.DecoratorPatternAttribute
        Gallio.Framework.Pattern..::.TestDecoratorPatternAttribute
          MbUnit.Framework..::.CatchExceptionAttribute
            MbUnit.Framework..::.AssertExceptionAttribute

See Also