none
Test case result using TFS API

    Question

  • I am using TFS API to get data about Test Plan, Test Suites, Test Cases etc. I want to get result of each step executed in a Test Case. Is there a way to get it? 

    Thanks

     

    Tuesday, March 29, 2011 10:14 PM

Answers

  •  

    var tfsColl = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(uri);
    var tcmService = tfsColl.GetService<ITestManagementService>();
    var testProject = tcmService.GetTeamProject("project name");
    
    // this example gets the suite on the first plan that matches the name "Suite Name", you can modify the query or iterate to find the one you want
    var plans = testProject.TestPlans.Query("SELECT * FROM TestPlan");
    var testSuite = plans[0].RootSuite.Entries.First(suite => suite.Title == "Suite Name").TestSuite;
    
    // iterate each test case
    foreach (ITestSuiteEntry suiteEntry in testSuite.TestCases)
    {
     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 actionResult = topIteration.Actions[actionIndex];

    string comment = actionResult.Comment;

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

     


    -Aidan
    • Marked as answer by KShar Friday, April 08, 2011 8:51 PM
    Tuesday, April 05, 2011 1:09 PM

All replies

  • Hi there

     

    You need to use the ITestActionResult interface - see http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.testmanagement.client.itestactionresult_members.aspx


    Please mark as answer if this helps.
    Friday, April 01, 2011 12:39 AM
  • I want to add few more details to my question. While executing a test case, I enter comments and screen shot for each step. I want to capture that data using the TFS API. Is it possible using the ITestActionResult interface?

     

    -Thanks

    Monday, April 04, 2011 7:53 PM
  •  

    var tfsColl = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(uri);
    var tcmService = tfsColl.GetService<ITestManagementService>();
    var testProject = tcmService.GetTeamProject("project name");
    
    // this example gets the suite on the first plan that matches the name "Suite Name", you can modify the query or iterate to find the one you want
    var plans = testProject.TestPlans.Query("SELECT * FROM TestPlan");
    var testSuite = plans[0].RootSuite.Entries.First(suite => suite.Title == "Suite Name").TestSuite;
    
    // iterate each test case
    foreach (ITestSuiteEntry suiteEntry in testSuite.TestCases)
    {
     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 actionResult = topIteration.Actions[actionIndex];

    string comment = actionResult.Comment;

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

     


    -Aidan
    • Marked as answer by KShar Friday, April 08, 2011 8:51 PM
    Tuesday, April 05, 2011 1:09 PM
  • Thank Aidan! I was working along the same lines. The issue is that  the line,

    string comment = actionResult.Comment;

    does not provide the comments entered for each step while the test was being executed. Moreover, is there a way to capture "Expected Result" for steps that have any?

     

     

    Friday, April 08, 2011 8:51 PM
  • I figured out how to get Expected Result for the step but the individual comments for each step still remains an issue for me.

     

    Thanks

    Friday, April 08, 2011 9:02 PM
  • I inserted a check to find if "actionResult.Comment" is returning Null. In my case, it returns Null for each step where as every step has a comment in it. Moreover, I am also not sure if "actionResult.Comment" would get me what I want.

    Any assistance is highly appreciated.

    Thanks

     

    Monday, April 11, 2011 4:12 PM
  • Hi KShar

    Replace your line

     

    actionResult.Comment

    with

    actionResult.ErrorMessage

     

    This should give you the result you need.

    Thanks

    Rob


    Please mark as answer if this helps.
    • Proposed as answer by Rob26MVP Monday, April 11, 2011 11:22 PM
    Monday, April 11, 2011 11:21 PM
  • Thanks Rob! It worked for me. So, the comments that are entered for each step while the test case is being executed, are treated as error messages rather than step-comments.

     

     

    Tuesday, April 12, 2011 1:49 PM