简体   繁体   中英

Retrieve TFS Test Steps always returns count of 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?

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();
        }
    }
}

在此处输入图片说明

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:

函数评估需要一个线程来运行

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM