使用 TFS API 克隆 VSTS/TFS 2015 测试套件






4.50/5 (2投票s)
如何克隆带有测试用例引用的测试套件。
引言
已经有复制测试计划、套件和测试用例的方法,比如你可以使用 批量复制工具。但对于我们的情况,我们不想创建测试用例的新副本。这种设置将创建所有套件的新实例,并且每个套件都会引用现有的测试用例,而不是创建测试用例的新副本。
在本文的最后,我还包括了如何克隆 Suites 和测试用例,以便如果你想创建自己的副本或将其包含在你自己的 TFS API 库中,可以获得每个套件和测试用例的新实例。
背景
我们有一个名为“Base Test Sets”的测试计划,其中包含代表不同测试集的套件。例如,它包含“回归测试”、“冒烟测试”等套件。每次发布或补丁时,我们都会运行不同的测试集。我们将场景所需的套件复制到该版本的测试计划中。
为了详细说明我们的测试系统以及需求的原因,我们为我们软件的每个版本/发布创建一个测试计划。在计划中,我们为每个 sprint、候选发布、正式发布和补丁都有一个套件。每个补丁版本都会获得需要运行的相同测试用例集,但如果我们要获得测试用例的新副本,那么每个补丁或发布都会有很多测试用例。这可以使用 MTM 完成,但并非所有人都能够访问 MTM,而且这可能会有点麻烦。
警告
请自行承担使用此代码的风险。下面概述的系统在我们的当前系统上运行没有问题,但修改代码或出现错误可能会导致您的 TFS 系统出现问题。请自行承担风险,就像您在直接使用 API 时的任何时候一样。我测试了下面在我们的普通 TFS 系统的开发环境克隆中的代码,以确保安全。
要求
您将不得不添加对 TFS DLL 的引用。为此,我正在使用 Visual Studio 12 中的 DLL。大多数 DLL 可以在 C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies 中找到。
入门
我一直将 TFS API 的所有代码添加到名为 TFSWrapper 的项目中,这是一个可供 UI 项目或 Windows 服务项目或任何需要使用的库。
以下 using 语句可以放在您喜欢的类中,但我将其放在前面提到的项目中,该项目包含我的所有 TFS API 方法。
using Microsoft.TeamFoundation.TestManagement.Client;
获取团队项目
首先,我们需要获取测试管理服务并调用 GetTeamProject 以获取 ITestManagementTeamProject 的实例。这些值是硬编码的,但您可以将变量放入配置或其他数据存储系统中。
 public static ITestManagementTeamProject GetTestmanagementProject(  
     string projectName)  
 {  
    NetworkCredential networkCredentials =   
    new NetworkCredential("username", "password","domain");  
    var tfsUri = https://tfs.company.com/tfs";  
    var myTFSTeamProjectCollection = new TfsTeamProjectCollection(  
    TfsTeamProjectCollection.GetFullyQualifiedUriForName(tfsUri),   
    networkCredentials);  
    ITestManagementService tms =   
       myTFSTeamProjectCollection.GetService<ITestManagementService>;  
    return tms.GetTeamProject(projectName);  
 }
获取测试套件
现在我们可以访问测试管理团队项目了,我们可以开始使用测试项。由于我们要克隆测试套件,我们需要使用其 Id 来检索它。我们将设置一个方法,可以通过其 Id 获取测试套件。
public static ITestSuiteBase GetTestSuite(int id, string teamProjectName)  
{  
     return GetTestManagementProject(teamProjectName).TestSuites.Find(id);  
}
开始复制
接下来,我们从 Id 获取 suites,然后开始克隆。
public static IStaticSuiteBase CopyStaticTestSuiteWithNewEntries
       (int fromSuiteID, int toSuiteID, string teamProjectName)  
     {  
       //Get the Test suites from the Ids
       var suiteToClone = 
           (IStaticTestSuite)GetTestSuite(fromSuiteID, teamProjectName);  
       var suiteToCloneInto = 
           (IStaticTestSuite)GetTestSuite(toSuiteID, teamProjectName);  
       
       //Only copy static suites.
       if(suiteToClone.TestSuiteType != TestSuiteType.StaticTestSuite)  
       {  
           throw new NotSupportedException("Only Static Test Suites Can be Cloned.");  
       }  
       //Clone the suite
       var clone = CopyStaticSuite(suiteToClone, suiteToCloneInto);  
       return clone;  
     }
递归复制子项
我们需要克隆套件和所有子套件,因此我们将设置一个递归方法。这有一个外部方法调用,我们将在后面介绍,但现在,只需按照注释操作即可。
public static IStaticTestSuite CopyStaticSuite
(IStaticTestSuite originalTestSuite, IStaticTestSuite testSuiteToCloneInto)  
 {  
       //Placeholder for the clone
       IStaticTestSuite clone; 
     
       //Clone the suite
       clone = CreateStaticSuiteWithoutChildren(
               originalTestSuite.Project.TeamProjectName, 
               originalTestSuite, testSuiteToCloneInto); 
       //Add the clone to the suite we are copying to, this creates the new entry
       testSuiteToCloneInto.Entries.Add(clone);  
       //Iterate through all of the entries being suites and test cases
       foreach (var entry in originalTestSuite.Entries)  
       {  
           //Add the test case reference to the clone
           if (entry.EntryType == TestSuiteEntryType.TestCase)  
           {  
               clone.TestCases.AddCases(new[] { entry.TestCase });  
           }  
           //Recursively add the child static suites
           else if(entry.EntryType == TestSuiteEntryType.StaticTestSuite)  
           {  
               CopyStaticSuite((IStaticTestSuite)entry.TestSuite, clone);  
           }  
       }  
       //The clone has the suites and test cases attached
       return clone;  
}  
克隆一个套件
现在创建 CreateStaticSuiteWithoutChildren 方法,之所以这样命名,是因为我的系统可以克隆带有子项的测试套件。在这里,我们只想创建一个新的静态测试套件并复制设置。
 public static IStaticTestSuite CreateStaticSuiteWithoutChildren(
     string projectName, IStaticTestSuite suiteToclone)  
{  
       IStaticTestSuite newSuite = 
       GetTestManagementProject(projectName).TestSuites.CreateStatic();  
       newSuite.Title = suiteToclone.Title;
       newSuite.Description = suiteToclone.Description;  
       return newSuite;  
}  
用法
现在我们拥有了所有我们需要的东西,我将保留设置 UI 或控制台应用程序或您希望如何使用该系统。要调用该代码,您只需要传入套件的 Id 和测试计划的名称。您可以使用返回的值进行日志记录和消息传递。
var suiteToCopyId = 123;
var suiteToCopyToId = 456;
var projectName = "default";
var clonedSuite = CopyStaticTestSuiteWithNewEntries
                  (suiteToCopyId, suiteToCopyToId, projectName);
console.WriteLine($"New Suite {clonedSuite.Id} cloned from Suite 
                 {suiteToCopyId} into Suite {suiteToCopyToID}");
额外:使用套件复制子项
为了在此基础上使用已经设置的内容,您可以轻松克隆套件并使用以下代码克隆测试用例。实际上,克隆带有子项的套件比没有子项的套件更容易,因为有一个内置方法。
 public static CloneOperationInformation CloneTestSuiteAndCloneTests
        (string projectName, int sourceSuiteId, int destinationSuiteId)  
     {  
       CloneOptions options = new CloneOptions();  
       var testManagementProject = GetTestManagementProject(projectName);  
       var testSuites = testManagementProject.TestSuites;  
       var result = testSuites.BeginCloneOperation
                    (sourceSuiteId, destinationSuiteId, options, projectName);  
       var info = testSuites.GetCloneOperationInformation(result);  
       return info;  
     }  
相关
要克隆工作项,请参阅文章 TFSAPICloningWorkItemswithChildren。

