0

I am trying to retrieve test steps (aka "actions") that have been added to a test case in TFS (2017.2) using the API (Microsoft.TeamFoundationServer.ExtendedClient v15.112.1). My current implementation always returns 0 test steps, although the actual test case has steps. I tried this as well in a clean new Team Project without any Work Item customization and even there it returns 0 steps. My implementation uses the older API (based on SOAP webservices), because it seems the newer http based API does not yet implement test steps. This is the code I have used:

private void GetTestStepsForTestCase(int testCaseId, int testSuiteId, 
string teamProjectName, Uri tfsUrl)
{
   TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(tfsUrl);
   ITestManagementService itms = tpc.GetService<ITestManagementService>();
   ITestManagementTeamProject ittp = itms.GetTeamProject(teamProjectName);
   ITestSuiteBase suite = ittp.TestSuites.Find(testSuiteId);
   ITestCaseCollection testCaseCollection = suite.AllTestCases;
   ITestCase itestCase = testCaseCollection.FirstOrDefault(t => t.Id == testCaseId);

   foreach (Microsoft.TeamFoundation.TestManagement.Client.ITestAction itestAction in itestCase.Actions)
   {
      // Do something
   }
}

Anyone?

Hubii
  • 348
  • 1
  • 14
Fokko
  • 188
  • 1
  • 14

2 Answers2

0

You can use below sample to retrieve the test case steps from a specific test suite, it works on my side:

Install the nuget package : Microsoft.TeamFoundationServer.ExtendedClient - 15.112.1

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.VisualStudio.Services.Client;
using System;

namespace RetrieveTestSteps
{
    class Program
    {
        static void Main(string[] args)
        {
            var u = new Uri("http://server:8080/tfs/DefaultCollection");
            var c = new VssClientCredentials();
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(u, c);
            tpc.EnsureAuthenticated();   
            ITestManagementService itms = tpc.GetService<ITestManagementService>();
            ITestManagementTeamProject ittp = itms.GetTeamProject("LCScrum");
            ITestSuiteBase suite = ittp.TestSuites.Find(352);
            ITestCaseCollection testCaseCollection = suite.AllTestCases;

            foreach (var tc in testCaseCollection)
            {
                ITestCase testcase = ittp.TestCases.Find(tc.Id);

                foreach (ITestAction action in testcase.Actions)
                {
                    Console.WriteLine(String.Format("{0} - {1}", testcase.Id, action));
                }
            }
            Console.Read();
        }
    }
}

enter image description here

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • Thanks for your answer Andy. I copied the code sample to a new console application (.NET Framework 4.6.1), but I still have the same problem. Probably something environment-related? – Fokko May 08 '18 at 12:00
  • @Fokko Are you sure you checked the correct suite or test case? I suggest you creating a new project, then create a test case to check if the issue still there. You can also try the code in another machine. I tested this on different machines, they all worked as expected. – Andy Li-MSFT May 09 '18 at 09:36
  • @Fokko You can try checking the specific test case with `ITestCase testcase = ittp.TestCases.Find(22);` to see if you can retrieve the test steps for the specific test case. – Andy Li-MSFT May 09 '18 at 09:40
0

OK, I finally figured out this one myself. The answer and comments of Andy helped me validate that my code is correct. I just discovered that my code worked fine when NOT debugging! When debugging, at some point I noticed this:

The function evaluation requires a threads to run

So probably because of lazy loading somewhere, it is not possible to verify the count of the attachments debug-time (see post here: Lazy<T>: "The function evaluation requires all threads to run").

Fokko
  • 188
  • 1
  • 14