如何从测试用例中获取父工作项?

时间:2017-11-10 23:19:42

标签: c# tfs tfs2010 tfs-sdk

我需要从测试用例中获取仅与具有特定FieldDefinition值的父工作项相关联的信息。

我一直在尝试做的是从测试管理服务获取项目列表,遍历所有测试用例,并从那里,希望找到与测试用例相关联的父工作项,并查看是否它有我正在寻找的FieldDefinition值。这是我到目前为止所做的,但是一旦我得到测试用例,我不知道从那里去哪里A)获取父工作项和B)查询FieldDefinitions以找到FieldDefinition值我'我正在寻找。文档非常模糊:

 public class TfsTest
  {
     private TfsTeamProjectCollection teamProjectCollection;
     private WorkItemStore workItemStore;
     private delegate void Execute();

     public void GenerateReport()
     {
        TeamProjectPicker teamProjectPicker = new TeamProjectPicker(TeamProjectPickerMode.NoProject, false);
         teamProjectPicker.ShowDialog();
        if (teamProjectPicker.SelectedTeamProjectCollection != null)
        {
           this.teamProjectCollection = teamProjectPicker.SelectedTeamProjectCollection;
           ITestManagementService testManagementService = (ITestManagementService)this.teamProjectCollection.GetService(typeof(ITestManagementService));

           this.workItemStore = (WorkItemStore)teamProjectCollection.GetService(typeof(WorkItemStore));

           foreach (Project project in workItemStore.Projects)
           {
              ITestManagementTeamProject _testproject = testManagementService.GetTeamProject(project.Name);
              GetTestPlans(_testproject);
           }
        }
     }

     void GetTestPlans(ITestManagementTeamProject testProject)
      {
        ITestPlanCollection plans = testProject.TestPlans.Query("Select * From TestPlan");

        foreach (ITestPlan plan in plans)
        {
           var planName = plan.Name;
           var testPlanType = ItemTypes.TestPlan;

           Console.WriteLine(string.Format("* {0} ({1})", plan.Name, testPlanType));

           if (plan.RootSuite != null && plan.RootSuite.Entries.Count > 0)
           {
              GetPlanSuites(plan.RootSuite.Entries, testProject);
           }
        }
     }

     void GetPlanSuites(ITestSuiteEntryCollection suites, ITestManagementTeamProject testProject)
     {
        foreach (ITestSuiteEntry suite_entry in suites)
        {
           IStaticTestSuite suite = suite_entry.TestSuite as IStaticTestSuite;
           if (suite != null)
           {

              // Test suites...
              var suiteTitle = suite.Title;
              var testSuiteType = ItemTypes.TestSuite;

              Console.WriteLine(string.Format("\t- {0} ({1})", suiteTitle, testSuiteType));

              GetTestCases(suite, testProject);
           }
        }
     }

     void GetTestCases(IStaticTestSuite suite, ITestManagementTeamProject testProject)
      {
        // iterate each test case
        foreach (ITestSuiteEntry suiteEntry in suite.TestCases)
        {
           if (suiteEntry.Id == 73649)
           {
              Console.WriteLine("Found it!");
           }

           var testResults = testProject.TestResults.ByTestId(suiteEntry.TestCase.Id);

           // iterate each result for the case
           foreach (ITestCaseResult result in testResults)
           {
              for (int actionIndex = 0; actionIndex < suiteEntry.TestCase.Actions.Count; actionIndex++)
              {
                 var action = suiteEntry.TestCase.Actions[actionIndex];
                 if (!(action is ITestStep))
                 {
                    continue;
                 }

                 var step = action as ITestStep;

                 var topIteration = result.Iterations.FirstOrDefault();
                 if (topIteration == null)
                 {
                    continue;
                 }

                 var actionCount = topIteration.Actions.Count();
                 if (actionCount == 0 || actionIndex > (actionCount - 1))
                 {
                    continue;
                 }

                 var actionResult = topIteration.Actions[actionIndex];

                 string comment = actionResult.Comment;

                 var testCaseItemType = ItemTypes.TestCase;

                 // TODO: Find the parent and query
                 // Parent.Store.FieldDefinitions for
                 // FieldDefinigion.Name.Contains("Client(s)"))
                 // Where value == "OHIO"

                 Console.WriteLine(string.Format("\t\t- Action: {0} ({1})", step.Title, testCaseItemType));
                 Console.WriteLine(string.Format("\t\t- Expected Result: {0}", step.ExpectedResult));

                 foreach (var attachment in actionResult.Attachments)
                 {
                     //attachment.DownloadToFile(Path.Combine("C:\attachments", attachment.Name));
                 }
              }
           }
        }
     }
  }

  var tfsTest = new TfsTest();
  tfsTest.GenerateReport();

  Console.WriteLine();
  Console.WriteLine("Done.");
  Console.WriteLine("Press any key to exit...");
  Console.ReadKey();

1 个答案:

答案 0 :(得分:0)

最简单的方法是使用 WIQL 。您可以使用WorkItemStore.Query方法之一或Query对象查询任何工作项以及工作项之间的链接。这些查询使用类似于Transact-SQL的工作项查询语言(WIQL)。代码段如下:

   TfsTeamProjectCollection teamProjectCollection =
               new TfsTeamProjectCollection(collectionUri);
WorkItemStore workItemStore = teamProjectCollection.GetService<WorkItemStore>();

string queryString = "select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.Tags] from WorkItems where [System.TeamProject] = 'YourTeamProjectName' and [System.WorkItemType] <> '' and [System.ChangedDate] >= @today - 30 order by [System.Id]";

// Create and run the query.
Query query = new Query(workItemStore, queryString);
WorkItemCollection witCollection = query.RunQuery();

foreach (WorkItem workItem in witCollection)
{

      foreach (WorkItemLink wiLink in workItem.WorkItemLinks)

      {
             //find if the link type is parent/chind
            if (wiLink.LinkTypeEnd.Name.Equals("Parent")|| wiLink.LinkTypeEnd.Name.Equals("Child"))
             {
                       .......
             }
       }
 }

您可以根据您的请求更改 queryString ,例如针对特定的测试用例工作项,并具有特定的FieldDefinition值。有关详细信息,您还可以查看此问题的答案:Retrieving work items and their linked work items in a single query using the TFS APIs