Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ExpectedExceptionBaseAttribute Class

 

This is a base class for attributes that specify to expect an exception from a unit test.

Namespace:   Microsoft.VisualStudio.TestTools.UnitTesting
Assembly:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)

System::Object
  System::Attribute
    Microsoft.VisualStudio.TestTools.UnitTesting::ExpectedExceptionBaseAttribute
      Microsoft.VisualStudio.TestTools.UnitTesting::ExpectedExceptionAttribute

[AttributeUsageAttribute(AttributeTargets::Method, AllowMultiple = false, 
	Inherited = true)]
public ref class ExpectedExceptionBaseAttribute abstract : Attribute

NameDescription
System_CAPS_protmethodExpectedExceptionBaseAttribute()

Initializes a new instance of the ExpectedExceptionBaseAttribute class.

System_CAPS_protmethodExpectedExceptionBaseAttribute(String^)

Initializes a new instance of the ExpectedExceptionBaseAttribute class.

NameDescription
System_CAPS_protpropertyNoExceptionMessage

This API supports the product infrastructure and is not intended to be used directly from your code.

System_CAPS_protpropertyTestContext

This API supports the product infrastructure and is not intended to be used directly from your code.

System_CAPS_pubpropertyTypeId

(Inherited from Attribute.)

NameDescription
System_CAPS_pubmethodEquals(Object^)

(Inherited from Attribute.)

System_CAPS_protmethodFinalize()

(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

(Inherited from Attribute.)

System_CAPS_pubmethodGetType()

(Inherited from Object.)

System_CAPS_pubmethodIsDefaultAttribute()

(Inherited from Attribute.)

System_CAPS_pubmethodMatch(Object^)

(Inherited from Attribute.)

System_CAPS_protmethodMemberwiseClone()

(Inherited from Object.)

System_CAPS_protmethodRethrowIfAssertException(Exception^)

Throws the exception again if it is an AssertFailedException or an AssertInconclusiveException.

System_CAPS_pubmethodToString()

(Inherited from Object.)

System_CAPS_protmethodVerify(Exception^)

This API supports the product infrastructure and is not intended to be used directly from your code.

By implementing your own expected exception verification. you can specify additional information and requirements that the built-in methods of the ExpectedExceptionAttribute class cannot handle, such as the following:

  • Verifying the state of the exception.

  • Expecting more than one type of exception.

  • Displaying a custom message when a wrong type of exception is thrown.

  • Controlling the outcome of a negative test.

  • For more information about how to use attributes, see Extending Metadata Using Attributes.

Legacy Code Example

The following class contains the method to test.

using System;

namespace CSExample
{
    public class DivisionClass
    {
        private int fraction;

        public int Divide(int numerator, int denominator)
        {
            return numerator / denominator;
        }
    }
}

The following custom attribute class, ExpectedArithmeticException, derives from the ExpectedExceptionBaseAttribute class to verify an exception type and message. The ExpectedArithmeticException attribute verifies that a test method throws an exception of a type that is or derives from System.ArithmeticException. It also verifies that the exception message matches the specified exception message. The unit test will pass because it throws a DivideByZeroException, which derives from ArithmeticException, and the specified exception message matches the message of the exception that is thrown by the unit test.

using CSExample;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestProject1
{
    public sealed class ExpectedArithmeticException : ExpectedExceptionBaseAttribute
    {
        private string exceptionMessage;
        private string wrongExceptionMessage;

        public string WrongExceptionMessage
        {
            get
            {
                return wrongExceptionMessage;
            }
            set
            {
                wrongExceptionMessage = value;
            }
        }

        public ExpectedArithmeticException(string expectedExceptionMessage) : this(expectedExceptionMessage, "No exception was thrown.")
        {
        }

        public ExpectedArithmeticException(string expectedExceptionMessage, string noExceptionMessage)
            : base(noExceptionMessage)
        {
            exceptionMessage = expectedExceptionMessage;
            WrongExceptionMessage = "The exception that was thrown does not derive from System.ArithmeticException.";
        }

        protected override void Verify(System.Exception exception)
        {
            Assert.IsNotNull(exception);

            // Handle assertion exceptions from assertion failures in the test method, since we are not interested in verifying those
            base.RethrowIfAssertException(exception);

            Assert.IsInstanceOfType(exception, typeof(System.ArithmeticException), wrongExceptionMessage);
            Assert.AreEqual(exceptionMessage, exception.Message, "Could not verify the exception message.");
        }
    }


    [TestClass()]
    public class DivisionClassTest
    {

        /* This test will pass because it thows a System.DivideByZeroException which derives from System.ArithmeticException. */
        [TestMethod()]
        [ExpectedArithmeticException("Attempted to divide by zero.", "An exception was expected, but no exception was thrown.", WrongExceptionMessage = "The wrong type of exception was thrown.")]
        public void DivideTest()
        {
            DivisionClass target = new DivisionClass();
            int numerator = 5;
            int denominator = 0;
            int actual;
            actual = target.Divide(numerator, denominator);
        }

    }
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: