2015-10-13 33 views
1

我有一个项目,我们在不同的DLL中有不同种类的单元测试。我想知道使用一些VB/C#代码在整个解决方案中的单元测试总数以及测试类别,所有者名称等。查找不同DLL中的单元测试总数

其实,我每天需要准备的报告,在这里我需要显示多少个单元测试是由何人以及在哪些

我不想打开Visual Studio知道该写的。

这是用于单元测试的一个示例签名:

<TestMethod()> <Owner("OwnerName"), TestCategory("Category22")> 
Public Sub TestName() 
    ............ 
    ............ 
End Sub 
+0

此信息以某种方式存储在代码,对不对?有属性吗?测试项目只是方法吗?编译代码中的所有者名称是否在某处? (即 - 不是评论)。除非您可以向我们展示一个测试方法的最小但完整的例子及其所有相关的元数据,否则可能不会有任何答案即将出现。 – theB

+0

我用一个例子更新了主要问题。谢谢 – gsgill76

+0

希望你没有想到我忘了你。 :) – theB

回答

0

由于测试方法都记录使用.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.NonPublicBindingFlags.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); 
     } 
    } 
} 
相关问题