Share via


ExpectedExceptionBaseAttribute 类

这是指定单元测试应引发异常的特性的基类。

继承层次结构

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

命名空间:  Microsoft.VisualStudio.TestTools.UnitTesting
程序集:  Microsoft.VisualStudio.QualityTools.UnitTestFramework(在 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 中)

语法

声明
<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple := False, Inherited := True)> _
Public MustInherit Class ExpectedExceptionBaseAttribute _
    Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public abstract class ExpectedExceptionBaseAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Method, AllowMultiple = false, Inherited = true)]
public ref class ExpectedExceptionBaseAttribute abstract : public Attribute
[<AbstractClass>]
[<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false, Inherited = true)>]
type ExpectedExceptionBaseAttribute =  
    class
        inherit Attribute
    end
public abstract class ExpectedExceptionBaseAttribute extends Attribute

ExpectedExceptionBaseAttribute 类型公开以下成员。

构造函数

  名称 说明
受保护的方法 ExpectedExceptionBaseAttribute() 初始化 ExpectedExceptionBaseAttribute 类的新实例。
受保护的方法 ExpectedExceptionBaseAttribute(String) 初始化 ExpectedExceptionBaseAttribute 类的新实例。

页首

属性

  名称 说明
受保护的属性 NoExceptionMessage 基础结构。
受保护的属性 TestContext 基础结构。
公共属性 TypeId 当在派生类中实现时,获取该 Attribute 的唯一标识符。 (继承自 Attribute。)

页首

方法

  名称 说明
公共方法 Equals 基础结构。返回一个值,该值指示此实例是否与指定的对象相等。 (继承自 Attribute。)
受保护的方法 Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
公共方法 GetHashCode 返回此实例的哈希代码。 (继承自 Attribute。)
公共方法 GetType 获取当前实例的 Type。 (继承自 Object。)
公共方法 IsDefaultAttribute 当在派生类中重写时,指示此实例的值是否是派生类的默认值。 (继承自 Attribute。)
公共方法 Match 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。 (继承自 Attribute。)
受保护的方法 MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
受保护的方法 RethrowIfAssertException 如果异常为 AssertFailedExceptionAssertInconclusiveException,则将再次引发异常。
公共方法 ToString 返回表示当前对象的字符串。 (继承自 Object。)
受保护的方法 Verify 基础结构。

页首

显式接口实现

  名称 说明
显式接口实现私有方法 _Attribute.GetIDsOfNames 将一组名称映射为对应的一组调度标识符。 (继承自 Attribute。)
显式接口实现私有方法 _Attribute.GetTypeInfo 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 (继承自 Attribute。)
显式接口实现私有方法 _Attribute.GetTypeInfoCount 检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute。)
显式接口实现私有方法 _Attribute.Invoke 提供对某一对象公开的属性和方法的访问。 (继承自 Attribute。)

页首

备注

通过实现您自己的预期异常验证。 可指定 ExpectedExceptionAttribute 类无法处理的内置方法的其他信息和要求,具体以下:

  • 验证异常的状态。

  • 预期多个类型的异常。

  • 引发错误的异常类型时显示自定义消息。

  • 控制否定性测试的结果。

  • 有关如何使用特性的更多信息,请参见利用特性扩展元数据

示例

下面的类包含要测试的方法。

using System;

namespace CSExample
{
    public class DivisionClass
    {
        private int fraction;

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

下面的自定义特性类 ExpectedArithmeticException 从 ExpectedExceptionBaseAttribute 类派生而来,用于验证异常类型和消息。 ExpectedArithmeticException 属性验证测试方法引发了一个异常 System.ArithmeticException 或由其派生类型的异常。 它还验证与指定的异常消息匹配的异常消息。 单元测试将通过,因为它将引发一个从 ArithmeticException 派生的 DivideByZeroException,并且指定的异常消息与单元测试引发的异常消息相匹配。

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);
        }

    }
}

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

请参见

参考

Microsoft.VisualStudio.TestTools.UnitTesting 命名空间