Saturday, April 20, 2013

How To Create a Test Plans and Cases Migration Tool For TFS : Part 3 - Duplicating Shared steps and action steps.

In my last post - How To Create a Test Plans and Cases Migration Tool For TFS : Part 2,  I showed how to migrate Test plans, suits, cases from one project to another, programmatically using Test Management API. In this post I'll show how to copy test steps of each test cases including shared steps. 

First of all we iterate all test steps of a test case. Then we try to see whether there is a shared reference for that test step. if it has an reference then it is a shared step, otherwise it's just a normal test step.

Code for duplication of test steps:
                   
                foreach (ITestAction action in testcase.TestCase.Actions)
                {

                    ISharedStep shared_step = null;
                    ISharedStepReference shared_ref = action as ISharedStepReference;
                    if (shared_ref != null)
                    {
                        shared_step = shared_ref.FindSharedStep();
                        foreach (ITestAction shr_action in shared_step.Actions)
                        {
                            ITestStep test_step = shr_action as ITestStep; ;
                            ITestStep destinationStep = tc.CreateTestStep();
                            destinationStep.Title = test_step.Title;
                            destinationStep.ExpectedResult = test_step.ExpectedResult;
                            tc.Actions.Add(destinationStep);
                        }

                    }
                    else
                    {
                        var test_step = action as ITestStep;
                        ITestStep destinationStep = tc.CreateTestStep();
                        destinationStep.Title = test_step.Title;
                        tc.Actions.Add(destinationStep);
                    }
                }



Full Code:

 public partial class MainWindow : Window
    {
        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;    

        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;
               
                 foreach (ITestAction action in testcase.TestCase.Actions)
                {
                    ISharedStep shared_step = null;
                    ISharedStepReference shared_ref = action as ISharedStepReference;
                    if (shared_ref != null)
                    {
                        shared_step = shared_ref.FindSharedStep();
                        foreach (ITestAction shr_action in shared_step.Actions)
                        {
                            ITestStep test_step = shr_action as ITestStep; ;
                            ITestStep destinationStep = tc.CreateTestStep();
                            destinationStep.Title = test_step.Title;
                            destinationStep.ExpectedResult = test_step.ExpectedResult;
                            tc.Actions.Add(destinationStep);
                        }

                    }
                    else
                    {
                        var test_step = action as ITestStep;
                        ITestStep destinationStep = tc.CreateTestStep();
                        destinationStep.Title = test_step.Title;
                        tc.Actions.Add(destinationStep);
                    }
                }
                tc.Save();
                destinationsuite.Entries.Add(tc);
            }
        }

        private void btn_connect_Click(object sender, RoutedEventArgs e)
        {
            CopyTestPlans();
        }
        
    }


1 comment:

  1. foreach (var action in originalTestCase.Actions)
    newTestCase.Actions.Add(action.CopyToNewOwner(newTestCase));

    ReplyDelete