Freigeben über


Find all test suites / test plans a test case belongs to

One of the most common asks on the visual studio user voice website has been to have a way to find out in what all test suites / test plans a test case is located (link). You can use the attached tool to find this.

The usage of the tool is :
FindTestCase <TestCaseId> <ServerUrl> <ProjectName>

TestCaseId - The id of the test case to be found.Eg : 515
ServerUrl- The server url . Eg : https://localhost:8080/tfs/DefaultCollection
ProjectName - The project name 

 

I present some sample code below to help with this issue. The code is fairly straightfoward. Do let me know if you have any questions / find any bugs in the code.

 

 namespace FindTestCase 
 { 
using Microsoft.TeamFoundation.Client; 
using Microsoft.TeamFoundation.TestManagement.Client; 
using System; 
using System.Linq; 

class FindTestCase 
{ 
static void Main(string[] args) 
{ 
int testCaseId; 
 if (args.Count() < 3)
            {
                Console.WriteLine("Usage : FindTestCase <TestCaseId> <ServerUrl> <ProjectName>");
                return;
            }

            if (!int.TryParse(args[0], out testCaseId))
            {
                Console.WriteLine("The test case id must be an 
 integer");
                return;
            }
            string serverUrl = args[1];
            string projectName = args[2];

            var projectCollectionUri = new Uri(serverUrl);
            var projectCollection = TfsTeamProjectCollectionFactory.
            GetTeamProjectCollection(projectCollectionUri);
            ITestManagementTeamProject project = projectCollection.
            GetService<ITestManagementService>().GetTeamProject(
            projectName);

            foreach (ITestPlan testPlan in project.TestPlans.Query(
            "SELECT * from TestPlan"))
            {
                //Check if the test case is present in the plan
                if (testPlan.RootSuite.AllTestCases.Where(tc => tc.Id 
                == testCaseId).Count() > 0)
                {
                    //If the test case is present, dig deeper to find 
 the suite(s) where it is present
                    FindInSuites(testPlan.RootSuite, 
                    testCaseId);
                }
            }
        }

        static void FindInSuites(IStaticTestSuite testSuite, int testCaseId)
        {
            foreach (ITestSuiteEntry entry in testSuite.Entries)
            {
                if (entry.EntryType == TestSuiteEntryType.TestCase)
                {
                    if (entry.Id == testCaseId)
                    {
                        Console.WriteLine("Test suite Id : {0}, Test 
 suite title : {1}, Test plan id : {2}", 
                        testSuite.Id, testSuite.Title, testSuite.Plan.
                        Id);
                    }
                }
                else
                {
                    IStaticTestSuite staticSuite = entry.TestObject as 
                    IStaticTestSuite;

                    if (staticSuite != null)
                    {
                FindInSuites(staticSuite,testCaseId);
                        
                    }
                    else
                    {
                        //Check in query based or requirement based 
 suites which cannot contain another suite as 
 child
                        IDynamicTestSuiteBase dynamicSuite = entry.
                        TestObject as IDynamicTestSuiteBase;
                        if (dynamicSuite != null)
                        {
                            if (dynamicSuite.TestCases.Where(tc => tc.
                            Id == testCaseId).Count() > 0)
                            {
                                Console.WriteLine("Test suite Id : {0},
 Test suite title : {1}, Test plan id : 
                                {2}", dynamicSuite.Id, dynamicSuite.
                                Title, dynamicSuite.Plan.Id);
                            }
                        }
                    }
                }
            }
        }
    }
}

FindTestCase.zip