In my last post -How To Create a Test Plans and Cases Migration Tool For TFS : Part 1, I showed how to create and manage Test plans, suits, cases programmatically using Test Management API. The knowledge of basics is necessary in order to understand the functionality of the Test Plans and Cases migration tool we intended to build. In this post let's focus on about how to get all the test suits and test cases in a test plan. This is bit tricky as test suits can be nested within test suits.
Once again you’ll need to add references to the following assemblies to your project.
- Microsoft.TeamFoundation.Client.dll
- Microsoft.TeamFoundation.TestManagement.Client.dll
- Microsoft.TeamFoundation.TestManagement.Common.dll
- Microsoft.TeamFoundation.WorkItemTracking.Client.dll
These assemblies are located at:C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0
Step 1: Connect to source and target projects
string sourceserverurl = "http://gandalf:8080/tfs/defaultcollection";
string sourceproject = "Sample Project";
string destinationtfs = "http://aragon:8080/tfs/defaultcollection";
string destinationproject = "PMTest";
ITestManagementTeamProject sourceproj;
ITestManagementTeamProject destinationproj;
sourceproj = GetProject(sourceserverurl, sourceproject);
destinationproj = GetProject(destinationtfs, destinationproject);
ITestManagementTeamProject GetProject(string serverUrl, string project)
{
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName(serverUrl));
ITestManagementService tms = tfs.GetService<ITestManagementService>();
return tms.GetTeamProject(project);
}
Step 2: Copy Test Plans from source to target project
foreach (ITestPlan sourceplan in sourceproj.TestPlans.Query("Select * From TestPlan"))
{
ITestPlan destinationplan = destinationproj.TestPlans.Create();
destinationplan.Name = sourceplan.Name;
destinationplan.Description = sourceplan.Description;
destinationplan.StartDate = sourceplan.StartDate;
destinationplan.EndDate = sourceplan.EndDate;
destinationplan.State = sourceplan.State;
destinationplan.Save();
}
Step 3: Check whether any root test suits exist for a test plan
if (sourceplan.RootSuite != null && sourceplan.RootSuite.Entries.Count > 0)
{
CopyTestSuites(sourceplan, destinationplan);
}
Step 4: Copy Root Test suits from source to target plan
void CopyTestSuites(ITestPlan sourceplan, ITestPlan destinationplan)
{
ITestSuiteEntryCollection suites = sourceplan.RootSuite.Entries;
foreach (ITestSuiteEntry suite_entry in suites)
{
IStaticTestSuite suite = suite_entry.TestSuite as IStaticTestSuite;
if (suite != null)
{
IStaticTestSuite newSuite = destinationproj.TestSuites.CreateStatic();
newSuite.Title = suite.Title;
destinationplan.RootSuite.Entries.Add(newSuite);
destinationplan.Save();
//Check whether root suite contains nested suits
CopyTestCases(suite, newSuite);
if (suite.Entries.Count > 0)
CopySubTestSuites(suite, newSuite);
}
}
}
Step 5: Recursively copy nested Test suits
void CopySubTestSuites(IStaticTestSuite parentsourceSuite, IStaticTestSuite parentdestinationSuite)
{
ITestSuiteEntryCollection suitcollection = parentsourceSuite.Entries;
foreach (ITestSuiteEntry suite_entry in suitcollection)
{
IStaticTestSuite suite = suite_entry.TestSuite as IStaticTestSuite;
if (suite != null)
{
IStaticTestSuite subSuite = destinationproj.TestSuites.CreateStatic();
subSuite.Title = suite.Title;
parentdestinationSuite.Entries.Add(subSuite);
// call to copy test cases that contains in the leaf test suite.
CopyTestCases(suite, subSuite);
// check whether if test suit contains nested test suits
if (suite.Entries.Count > 0)
CopySubTestSuites(suite, subSuite);
}
}
}
Step 6: Copy Test cases from source to target plan
void CopyTestCases(IStaticTestSuite sourcesuite, IStaticTestSuite destinationsuite)
{
ITestCaseCollection dd=sourcesuite.AllTestCases;
ITestSuiteEntryCollection suiteentrys = sourcesuite.TestCases;
foreach (ITestSuiteEntry testcase in suiteentrys)
{
ITestCase tc = destinationproj.TestCases.Create();
tc.Title = testcase.Title;
tc.Priority = testcase.TestCase.Priority;
tc.Save();
destinationsuite.Entries.Add(tc);
}
}
Full Code:
public partial class MainWindow : Window
{
string sourceserverurl = "http://gandalf:8080/tfs/defaultcollection";
string sourceproject = "Sample Project";
string destinationtfs = "http://zeus:8080/tfs/defaultcollection";
string destinationproject = "PMTest";
ITestManagementTeamProject sourceproj;
ITestManagementTeamProject destinationproj;
public MainWindow()
{
InitializeComponent();
sourceproj = GetProject(sourceserverurl, sourceproject);
destinationproj = GetProject(destinationtfs, destinationproject);
}
private ITestManagementTeamProject GetProject(string serverUrl, string project)
{
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName(serverUrl));
ITestManagementService tms = tfs.GetService<ITestManagementService>();
return tms.GetTeamProject(project);
}
void CopyTestPlans()
{
foreach (ITestPlan destinationplan in destinationproj.TestPlans.Query("Select * From TestPlan"))
{
System.Diagnostics.Debug.WriteLine("Deleting Plan - {0} : {1}", destinationplan.Id, destinationplan.Name);
destinationplan.Delete(DeleteAction.ForceDeletion); ;
}
foreach (ITestPlan sourceplan in sourceproj.TestPlans.Query("Select * From TestPlan"))
{
System.Diagnostics.Debug.WriteLine("Plan - {0} : {1}", sourceplan.Id, sourceplan.Name);
ITestPlan destinationplan = destinationproj.TestPlans.Create();
destinationplan.Name = sourceplan.Name;
destinationplan.Description = sourceplan.Description;
destinationplan.StartDate = sourceplan.StartDate;
destinationplan.EndDate = sourceplan.EndDate;
destinationplan.State = sourceplan.State;
if (sourceplan.RootSuite != null && sourceplan.RootSuite.Entries.Count > 0)
{
CopyTestSuites(sourceplan, destinationplan);
}
destinationplan.Save();
}
}
void CopyTestSuites(ITestPlan sourceplan, ITestPlan destinationplan)
{
ITestSuiteEntryCollection suites = sourceplan.RootSuite.Entries;
foreach (ITestSuiteEntry suite_entry in suites)
{
IStaticTestSuite suite = suite_entry.TestSuite as IStaticTestSuite;
if (suite != null)
{
IStaticTestSuite newSuite = destinationproj.TestSuites.CreateStatic();
newSuite.Title = suite.Title;
destinationplan.RootSuite.Entries.Add(newSuite);
destinationplan.Save();
CopyTestCases(suite, newSuite);
if (suite.Entries.Count > 0)
CopySubTestSuites(suite, newSuite);
}
}
}
void CopySubTestSuites(IStaticTestSuite parentsourceSuite, IStaticTestSuite parentdestinationSuite)
{
ITestSuiteEntryCollection suitcollection = parentsourceSuite.Entries;
foreach (ITestSuiteEntry suite_entry in suitcollection)
{
IStaticTestSuite suite = suite_entry.TestSuite as IStaticTestSuite;
if (suite != null)
{
IStaticTestSuite subSuite = destinationproj.TestSuites.CreateStatic();
subSuite.Title = suite.Title;
parentdestinationSuite.Entries.Add(subSuite);
CopyTestCases(suite, subSuite);
if (suite.Entries.Count > 0)
CopySubTestSuites(suite, subSuite);
}
}
}
void CopyTestCases(IStaticTestSuite sourcesuite, IStaticTestSuite destinationsuite)
{
ITestCaseCollection dd=sourcesuite.AllTestCases;
ITestSuiteEntryCollection suiteentrys = sourcesuite.TestCases;
foreach (ITestSuiteEntry testcase in suiteentrys)
{
ITestCase tc = destinationproj.TestCases.Create();
tc.Title = testcase.Title;
tc.Priority = testcase.TestCase.Priority;
tc.Save();
destinationsuite.Entries.Add(tc);
}
}
private void btn_connect_Click(object sender, RoutedEventArgs e)
{
CopyTestPlans();
}
}
Next time let's discuss how to add action steps and duplicate shared steps of test cases in target server.