从visual studio 2013中提取测试用例

时间:2014-08-11 09:59:34

标签: c# visual-studio-2013

如何使用“TestClass”属性及其所有带有“TestMethods”属性的方法将所有类提取到外部文件中,如.txt或excel类型,而不使用TFS?

2 个答案:

答案 0 :(得分:2)

可能有工具,但这里是一个使用反射的简单实现:

var assembly = Assembly.LoadFile("xxx.dll");

var testClasses = assembly.GetTypes()
    .Where(c => c.GetCustomAttribute<TestClassAttribute>() != null);

foreach (var testClass in testClasses)
{
    Console.WriteLine("Found test class " + testClass.FullName);

    var testMethods = testClass.GetMethods().Where(m => m.GetCustomAttribute<TestMethodAttribute>() != null);
    foreach (var testMethod in testMethods)
    {
        Console.WriteLine("Found test method " + testMethod.Name);
    }
}

答案 1 :(得分:0)

[TestMethod]
public void list__all_unit_tests()
{
    string sDLL = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
    string sFilePath = string.Format("{0:s}\\{1:s}.DLL", System.IO.Directory.GetCurrentDirectory(), sDLL);
    var assembly = System.Reflection.Assembly.LoadFile(sFilePath);

    var testClasses = assembly.GetTypes()
                .Where(m => m.GetCustomAttributes(typeof(TestClassAttribute),false)!=null);

    foreach (var testClass in testClasses)
    {
        var testMethods = testClass.GetMethods()
                .Where(m => m.GetCustomAttributes(typeof(TestMethodAttribute),true).Length !=0);

        foreach (var testMethod in testMethods)
        {
            Debug.Print(string.Format("class,{0:s}, method,{1:s} ", testClass.FullName , testMethod.Name));
        }
    }
}