//Copy all Test suites from source plan to destination plan. private void CopyTestSuites(ITestPlan sourceplan, ITestPlan destinationplan) { ITestSuiteEntryCollection suites = sourceplan.RootSuite.Entries; CopyTestCases(sourceplan.RootSuite, destinationplan.RootSuite); foreach (ITestSuiteEntry suite_entry in suites) { if (suite_entry.TestSuite is IStaticTestSuite suite) { IStaticTestSuite newSuite = targetTestMgmtProj.TestSuites.CreateStatic(); newSuite.Title = suite.Title; destinationplan.RootSuite.Entries.Add(newSuite); destinationplan.Save(); CopyTestCases(suite, newSuite); if (suite.Entries.Count > 0) { CopySubTestSuites(suite, newSuite); } } else { if (suite_entry.TestSuite is IDynamicTestSuite dynamicSuite) { IDynamicTestSuite newDynamicSuit = targetTestMgmtProj.TestSuites.CreateDynamic(); newDynamicSuit.Title = dynamicSuite.Title; //newDynamicSuit.Query = dynamicSuite.Query; var text = ReplaceAreaPath(dynamicSuite.Query.QueryText); text = ReplaceIterationPath(text); var newQuery = targetTestMgmtProj.CreateTestQuery(text); newDynamicSuit.Query = newQuery; destinationplan.RootSuite.Entries.Add(newDynamicSuit); destinationplan.Save(); } } } }
public void CreateSprint1TestPlan() { TfsTeamProjectCollection tpc = TfsConnect(); ITestManagementTeamProject project = tpc.GetService <ITestManagementService>().GetTeamProject(TeamProject); // Create test plan if none exist // // See http://bit.ly/2dup2XY for why we can't delete Test Plans or Suites at this point in time // // If this routine isn't creating the test plan and/or test suites for you, you'll need to manually // delete the Test Plan and Test Suites using witadmin WorkItemStore store = new WorkItemStore(tpc); string wiql = "SELECT [System.Id] FROM WorkItems " + "WHERE [System.TeamProject] = '" + TeamProject + "' AND [System.WorkItemType] ='Test Plan' AND [System.Title] = 'Sprint 1'"; WorkItemCollection workItems = store.Query(wiql); int testPlanCount = workItems.Count; wiql = "SELECT [System.Id] FROM WorkItems " + "WHERE [System.TeamProject] = '" + TeamProject + "' AND [System.WorkItemType] ='Test Suite'"; int testSuiteCount = store.Query(wiql).Count; ITestPlan testPlan; if (testPlanCount == 0) { testPlan = project.TestPlans.Create(); testPlan.Name = "Sprint 1"; testPlan.Iteration = @"Fabrikam\Release 1\Sprint 1"; testPlan.Save(); Console.WriteLine(" . (1 plan created)"); } else { testPlan = project.TestPlans.Find(workItems[0].Id); Console.WriteLine(" . (plan exists)"); } // Create Test Suites if non exist if (testSuiteCount <= 10) // May create duplicate test suites { Console.Write(" Creating sprint 1 test suites "); // suites int count = 0; IStaticTestSuite staticSuite = project.TestSuites.CreateStatic(); staticSuite.Title = "Automated"; testPlan.RootSuite.Entries.Add(staticSuite); testPlan.Save(); Console.Write("."); count++; staticSuite = project.TestSuites.CreateStatic(); staticSuite.Title = "Regression"; testPlan.RootSuite.Entries.Add(staticSuite); testPlan.Save(); Console.Write("."); count++; // Requirement-based suites // Get PBI work items wiql = "SELECT [System.Id] FROM WorkItems " + "WHERE [System.TeamProject] = '" + TeamProject + "' AND [System.WorkItemType] ='Product Backlog Item'"; workItems = store.Query(wiql); for (int i = 0; i < workItems.Count; i++) { WorkItem pbi = workItems[i]; // Link Test Case to PBI int testCaseID = (int)TestCases[pbi.Title.ToLower()]; WorkItemLinkTypeEnd testedByLink = store.WorkItemLinkTypes.LinkTypeEnds["Tested By"]; pbi.WorkItemLinks.Add(new WorkItemLink(testedByLink, testCaseID)); pbi.Save(); // Create Requirement-based test suite IRequirementTestSuite reqSuite = project.TestSuites.CreateRequirement(pbi); reqSuite.Title = pbi.Title; testPlan.RootSuite.Entries.Add(reqSuite); testPlan.Save(); Console.Write("."); count++; } // Query-based suites IDynamicTestSuite querySuite = project.TestSuites.CreateDynamic(); querySuite.Title = "UI Tests"; querySuite.Query = project.CreateTestQuery(@"SELECT [System.Id],[System.WorkItemType],[System.Title],[Microsoft.VSTS.Common.Priority],[System.AssignedTo],[System.AreaPath] FROM WorkItems WHERE [System.TeamProject] = @project AND [System.WorkItemType] IN GROUP 'Microsoft.TestCaseCategory' AND [System.AreaPath] UNDER 'Fabrikam' AND [System.IterationPath] UNDER 'Fabrikam\Release 1\Sprint 1' AND [System.Title] CONTAINS 'ui'"); testPlan.RootSuite.Entries.Add(querySuite); testPlan.Save(); Console.Write("."); count++; querySuite = project.TestSuites.CreateDynamic(); querySuite.Title = "Bug Existence Tests"; querySuite.Query = project.CreateTestQuery(@"SELECT [System.Id],[System.WorkItemType],[System.Title],[Microsoft.VSTS.Common.Priority],[System.AssignedTo],[System.AreaPath] FROM WorkItems WHERE [System.TeamProject] = @project AND [System.WorkItemType] IN GROUP 'Microsoft.TestCaseCategory' AND [System.AreaPath] UNDER 'Fabrikam' AND [System.IterationPath] UNDER 'Fabrikam\Release 1\Sprint 1' AND [System.Title] CONTAINS 'bug'"); testPlan.RootSuite.Entries.Add(querySuite); testPlan.Save(); Console.Write("."); count++; Console.WriteLine(string.Format(" ({0} suites created)", count)); } }