TestPropertyAttribute 类
更新:2007 年 11 月
在方法上建立测试特定属性。此类不能被继承。
命名空间: Microsoft.VisualStudio.TestTools.UnitTesting
程序集: Microsoft.VisualStudio.SmartDevice.UnitTestFramework(在 Microsoft.VisualStudio.SmartDevice.UnitTestFramework.dll 中)
语法
声明
<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple := True)> _
Public NotInheritable Class TestPropertyAttribute _
Inherits Attribute
用法
Dim instance As TestPropertyAttribute
[AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = true)]
public sealed class TestPropertyAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Method, AllowMultiple = true)]
public ref class TestPropertyAttribute sealed : public Attribute
public final class TestPropertyAttribute extends 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);
}
}
}
}
继承层次结构
System.Object
System.Attribute
Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute
线程安全
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。