Share via


TestPropertyAttribute 类

在方法上建立测试特定属性。 此类不能被继承。

继承层次结构

System.Object
  System.Attribute
    Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute

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

语法

声明
<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple := True)> _
Public NotInheritable Class TestPropertyAttribute _
    Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = true)]
public sealed class TestPropertyAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Method, AllowMultiple = true)]
public ref class TestPropertyAttribute sealed : public Attribute
[<Sealed>]
[<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = true)>]
type TestPropertyAttribute =  
    class
        inherit Attribute
    end
public final class TestPropertyAttribute extends Attribute

TestPropertyAttribute 类型公开以下成员。

构造函数

  名称 说明
公共方法 TestPropertyAttribute NameValue 属性的值初始化 TestPropertyAttribute 类的新实例。

页首

属性

  名称 说明
公共属性 Name 获取测试属性的名称。
公共属性 TypeId 当在派生类中实现时,获取该 Attribute 的唯一标识符。 (继承自 Attribute。)
公共属性 Value 获取测试属性的值。

页首

方法

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

页首

显式接口实现

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

页首

备注

若要查看属性,请先重新生成项目,然后突出显示**“测试列表编辑器”“测试视图”窗口中的测试。 重新生成项目后,可以在“属性”**窗格中查看属性。

可以在测试方法上指定此属性。 可以使用此特性的多个实例来指定多个项。

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

示例

下面的示例演示如何从 MyTestMethod() 方法内部访问 TestProperty 属性。

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Forms;
using System.Reflection;

namespace TestProperty
{
   [TestClass]
   public class TestPropertyTest
   {
      [TestMethod()]
      [TestProperty("MyProperty1", "Big")]
      [TestProperty("MyProperty2", "Small")]
      public void MyTestMethod()
      {
         // Get the current type
         Type t = GetType();

         MethodInfo mi = t.GetMethod("MyTestMethod");
         Type MyType = typeof(TestPropertyAttribute);
         object[] attributes = mi.GetCustomAttributes(MyType, false);

         for (int i = 0; i < attributes.Length; i++)
         {
            string name = ((TestPropertyAttribute)attributes[i]).Name;
            string val = ((TestPropertyAttribute)attributes[i]).Value;

            string mystring = string.Format("Property Name: {0}, Value: {1}", name, val);
            MessageBox.Show(mystring);
         }
      }
   }
}

线程安全

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

请参见

参考

Microsoft.VisualStudio.TestTools.UnitTesting 命名空间

MethodInfo

其他资源

使用单元测试框架对自定义单元测试进行编码