0

I need to get obtain information from Test Cases that are only associated with Parent Work Items that have a particular FieldDefinition value.

What I've been trying to do, is get a list of projects from the Test Management Service, iterating through all the Test Cases, and from there, hopefully, find the Parent Work Item associated with the Test Case and see if it has the FieldDefinition value I'm looking for. Here is what I have so far, but once I get my hands on the Test Cases, I don't know where to go from there to A) get the Parent Work Item and B) query the FieldDefinitions to find the FieldDefinition value I'm looking for. The documentation is pretty murky:

 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();
riQQ
  • 9,878
  • 7
  • 49
  • 66
gangelo
  • 3,034
  • 4
  • 29
  • 43

1 Answers1

0

The simplest way is using WIQL. You can query for any of work items, and links between work items by using one of the WorkItemStore.Query methods or a Query object. These queries use the work item query language (WIQL), which resembles Transact-SQL. A code snippet as below:

   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"))
             {
                       .......
             }
       }
 }

You could change the queryString according to your request such as for a specific test case work item and have a particular FieldDefinition value. For more details, you could also have a look at answers in this question: Retrieving work items and their linked work items in a single query using the TFS APIs

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62