使用TFS API通过Id获取特定的TestSuite

时间:2012-04-19 15:21:47

标签: tfs tfs-sdk

我正在尝试使用TFS API为TestPlan获取特定的TestSuite。

TestSuite可以存在于TestSuite层次结构中的任何位置,因此,当然我可以编写递归函数。我想要更高效的东西。

是否有我遗漏的方法,或者我可以编写的查询?

1 个答案:

答案 0 :(得分:5)

如果您已经知道testSuiteId事情非常简单。您只需要知道TeamProject的名称teamProjectName

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

namespace GetTestSuite
{
    class Program
    {
        static void Main()
        {
           int testSuiteId = 555;
           const string teamProjectName = "myTeamProjectName";

           var tpc =
                TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
                    new Uri("http://tfsURI"));

           var tstService = (ITestManagementService)tpc.GetService(typeof(ITestManagementService));
           var tProject = tstService.GetTeamProject(teamProjectName);

           var myTestSuite = tProject.TestSuites.Find(testSuiteId);            
        }
    }
}

如果你不这样做,你可能需要寻找类似于here(这是S.Raiten帖子)的解决方案,其中递归确实会出现。假设访问testPlanId

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

namespace GetTestSuite
{
    class Program
    {
        static void Main()
        {
            int testPlanId = 555;
            const string teamProjectName = "myTeamProjectName";

            var tpc =
                TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
                    new Uri("http://tfsURI"));

            var tstService = (ITestManagementService)tpc.GetService(typeof(ITestManagementService));
            var tProject = tstService.GetTeamProject(teamProjectName);

            var myTestPlan = tProject.TestPlans.Find(testPlanId);
            GetPlanSuites(myTestPlan.RootSuite.Entries);                
        }

        public static void GetPlanSuites(ITestSuiteEntryCollection suites)
        {
            foreach (ITestSuiteEntry suiteEntry in suites)
            {
                Console.WriteLine(suiteEntry.Id);
                var suite = suiteEntry.TestSuite as IStaticTestSuite;
                if (suite != null)
                {
                    if (suite.Entries.Count > 0)
                        GetPlanSuites(suite.Entries);
                }
            }
        }
    }
}