Click here to Skip to main content
14,979,362 members
Articles / DevOps
Article
Posted 18 Aug 2016

Stats

10.1K views
3 bookmarked

Clone VSTS/TFS 2015 Test Suite With TFS API

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
18 Aug 2016CPOL4 min read
Clone Test Suites with Test Case References

Introduction

There is already ways of copying test plans, suites and test cases like you can do with the Bulk Copy Tool.  But for our situation we do not want to make a new copy of the test case. This setup will create a new instance of all of the suites, and each suite gets a reference to existing test cases instead of making new copies of the test cases.

At the end of this I have also included how to clone the Suites and the Test Cases to get new instances of each if you want to make your own or include it in your own TFS API Library.

 

Background

We have a Test Plan called Base Test Sets which contains suites that represent different testing sets.  For instance it contains the suites Regression, Smoke ecteach release or patch we run through a different set of tests. We copy the suite needed for the scenario into a Test Plan for that version.

To elaborate on our tests system and the reason for the need, we make a test plan for each Version/Release of our software.  In the plan we have a suite for each sprint, for Release Candidate, for General Release and for patches.  The each patch release gets the same set of test cases that need to be run, but if we got new copies of the test cases each patch or release we would have a lot of test cases.  This can be done with MTM but not everyone has access to MTM and it can be a bit of a burden.

 

Warning

Use this code at your own risk. The system outlined below works on our current system without issue, but modifying the code or mistakes may cause issues to your TFS system. Use at your own risk as you are any time you work directly against the API. I tested the code below in a Development Environment clone of our normal TFS system to be safe.

 

Requirements

You will have to add references to the TFS DLLs. For this, I am using the DLLs from Visual Studio 12. Most of the DLLs can be found in C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies.

 

Getting Started

I have been adding all of the code for the TFS API into a project TFSWrapper which is a library that can be used by UI projects or windows service projects or whatever is needed.

The following using can be put in a class wherever you prefer but I put it in a the afforementioned project which holds all of my TFS API methods.  

C#
using Microsoft.TeamFoundation.TestManagement.Client;

 

Getting the Team project

First we need to get the Test Management Service and call GetTeamProject to get an instance of the ITestManagementTeamProject.  This has the values hard coded but you can put the variables into a config or other data storage system.

C#
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);
}

 

Getting the Test Suites

Now that we have access to the Test Management Team Project we can start working with the Test items.  Since we want to clone the Test Suite we need to retrieve it by using its Id.  We will setup a method that can get the Test Suite by its Id.

C#
public static ITestSuiteBase GetTestSuite(int id, string teamProjectName)  
{  
     return GetTestManagementProject(teamProjectName).TestSuites.Find(id);  
}  

 

Start Copying

Next we get the suites from the Id's, and start cloning.

C#
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;  
     }  

 

Recursive Copy Children

We need to clone the suite and all of the sub suites so we will setup a method that will be recursive.  This has an outside method call that we will get to next, but for now just follow along by the comments.

C#
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;  
}  

 

Clone a Suite

Now to create the CreateStaticSuiteWithoutChildren method, this is named this way as my system can clone the test suite with children.  Here we just want to make a new Static Test Suite and Copy the Settings over.

C#
 public static IStaticTestSuite CreateStaticSuiteWithoutChildren(
     string projectName, IStaticTestSuite suiteToclone)  
{  
       IStaticTestSuite newSuite = GetTestManagementProject(projectName).TestSuites.CreateStatic();  
       newSuite.Title = suiteToclone.Title;
       newSuite.Description = suiteToclone.Description;  
       return newSuite;  
}  

 

Usage

Now we have everything we need, I will leave setting up the UI or console application or however you want to use the system.  To call the code you just need to pass in the Id's for the suites and the name of the test plan.  You can use the returned value for logging and messaging.

C#
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}");

 

Extra: Copy Children with Suite

To add to this using what is already setup now you can easily clone the suite and clone the test cases with the code below.  Actually it is much easier to clone a suite with children than it is without as there is a built in method.

C#
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;
    }

 

Related

For cloning work Items see the article TFS API Cloning Work Items with Children

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Metaphysico
Software Developer
United States United States
I am currently an Automation Developer working on Coded UI Frameworks using Selenium, Load Testing Frameworks and Dynamic Data Frameworks for the prior mentioned testing systems. I maintain all of the software and hardware for our build, release and test systems, mostly using TFS, powershell and Hyper-V as well as building and maintaining Automated test systems.

Skills
SQL, Software Development, Microsoft SQL Server, Agile Methodologies
C#, JavaScript, HTML, C++, Markdown, XAML, XML
Quality Testing, Coded UI Testing, Selenium Testing, Load Testing
ALM, Team Foundation Server/VSO, Build Administration
Databases SQL/Oracle, Software and Hardware Installation/Maintenance
Troubleshooting, Project Management,
Solid State Electronics, Pulse Width Modulation, Metal Casting, Carbon Fiber Casting, Powder Coating.

Comments and Discussions

 
QuestionCopying TFS Testcases Pin
Member 147092747-Jan-20 0:50
MemberMember 147092747-Jan-20 0:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.