由于测试方法都记录使用.NET Framework属性,可以使用反射来得到所需要的信息。在VB.net中,你可以这样做,如下所示。
'Imports System.Reflection
'Imports TestLibrary <- wherever the attributes are defined
Dim assy As Assembly = Assembly.LoadFrom(filename)
Dim owner As OwnerAttribute
Dim category As TestCategoryAttribute
For Each t As Type In assy.GetTypes()
For Each mi As MethodInfo In t.GetMethods(BindingFlags.Public Or BindingFlags.Instance)
If Not mi.GetCustomAttributes(GetType(TestMethodAttribute)).Count() = 0 Then
owner = mi.GetCustomAttribute(Of OwnerAttribute)()
category = mi.GetCustomAttribute(Of TestCategoryAttribute)()
System.Diagnostics.Debug.Print("{0} : Owner='{1}' Category='{2}'",
mi.Name, owner.OwnerName, category.CategoryName)
End If
Next
Next
您必须添加测试框架DLL作为对项目的引用。 filename
是已编译程序集(EXE,DLL等)的完整路径。我没有包含空间原因的错误检查。
如果您的测试方法不公开,或者它是静态的,您可以通过分别将绑定标志更改为BindingFlags.NonPublic
或BindingFlags.Static
来获得它。 (更多信息可在该documentation可用。)
注意,此方法也适用于C#
Assembly assy = Assembly.LoadFrom(filename);
OwnerAttribute owner = null;
TestCategoryAttribute category = null;
foreach(Type t in assy.GetTypes())
{
foreach (MethodInfo mi in t.GetMethods(BindingFlags.Public | BindingFlags.Instance))
{
if (mi.GetCustomAttributes(typeof(TestMethodAttribute)).Count() != 0)
{
owner = mi.GetCustomAttribute<OwnerAttribute>();
category = mi.GetCustomAttribute<TestCategoryAttribute>();
System.Diagnostics.Debug.Print("{0} : Owner='{1}' Category='{2}'",
mi.Name, owner.OwnerName, category.CategoryName);
}
}
}
此信息以某种方式存储在代码,对不对?有属性吗?测试项目只是方法吗?编译代码中的所有者名称是否在某处? (即 - 不是评论)。除非您可以向我们展示一个测试方法的最小但完整的例子及其所有相关的元数据,否则可能不会有任何答案即将出现。 – theB
我用一个例子更新了主要问题。谢谢 – gsgill76
希望你没有想到我忘了你。 :) – theB