繁体   English   中英

从Visual Studio 2013中提取测试用例

[英]Extract test cases from visual studio 2013

如何在不使用TFS的情况下将具有“ TestClass”属性的所有类及其具有“ TestMethods”属性的所有方法提取到外部文件(如.txt或excel类型)中?

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

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);
    }
}
[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));
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM