In TFS, how do I find all Test Cases in a Test Suite with a query (C#)?

14,454

Solution 1

Unfortunately, there are no work item links created between Test Plans, Suites and Cases. So although they are Work Items, they don't have links. This means that a default query isn't possible.

A work around is tagging all test cases in a suite with the name of the suite. You can then use a query that filters on the work item tags.

You can go even further and automate the creation of tags by using some Web Hooks and Azure Functions (or some other hosted API) magic. This allows you to create a Web Hook that listens for the creation (or updates) to Test Cases. By using some of the code mentioned in the other posts you can retrieve the Test Suite of the Test Case and then use the REST API to add it as a Tag to the Test Case.

Solution 2

You may need to use this Interface ITestSuiteBase.

AllTestCases 

     Gets the read-only collection of test cases for this suite and all hierarchical children.

TestCases 

     Gets a read-only collection of test cases.

More info from MSDN

Here is a example code:

public static List<TestCase> GetAllTestCaseFromSuite(ITestPlan testPlan, int suiteId, bool includeExecutionStatus = true)
{
    List<TestCase> testCases = new List<TestCase>();
    testPlan.Refresh();
    ITestSuiteBase currentSuite = testPlan.Project.TestSuites.Find(suiteId);
    currentSuite.Refresh();
    foreach (var currentTestCase in currentSuite.TestCases)
    {
        TestCase testCaseToAdd = new TestCase(currentTestCase.TestCase, currentSuite, testPlan, includeExecutionStatus);
        if (!testCases.Contains(testCaseToAdd))
        {
            testCases.Add(testCaseToAdd);
        }
    }
    log.InfoFormat("Load all test cases in the suite with Title= \"{0}\" id = \"{1}\"", currentSuite.Title, currentSuite.Id);
    return testCases;
}

More details you can refer this blog: Manage TFS Test Cases C# Code

Solution 3

If you are using TFS 2015 or higher,

you can check this link :

  1. usage

  2. TestCaseExplorer Tool

  3. list-bugs-and-the-test-cases-that-test-them enter image description here

if Not using TFS 2015 or higher :

For now, there is no way to create ordinary TFS query via Web interface and not API call or custom coding to get list of Test Cases belongs to a specific Test Suite. support-querying-for-all-test-cases-in-a-specifed

Or try old tool : test-plans-test-suites-test-cases-mapping

Share:
14,454
whoknows
Author by

whoknows

merge me

Updated on July 20, 2022

Comments

  • whoknows
    whoknows almost 2 years

    With Team Foundation Server, given a WorkItem of type "Test Suite," how can I write a query to select all Test Cases associated to that Test Suite?

    • Chase
      Chase about 8 years
      Programmatically, look to this blog post. May hint at a solution that is done through queries only.
    • A Khudairy
      A Khudairy over 6 years
      you mean Programmatically or just using the Query builder tool? and are you using online TFS or on premises
    • whoknows
      whoknows over 6 years
      Query builder tool. And yep, using this both in the online interface and pulled down to a program.
  • whoknows
    whoknows about 8 years
    I need a query only based solution
  • whoknows
    whoknows over 6 years
    Thanks for the reply, but I don't see where it shows how to query for test cases under a given test suite. It is TFS 2015. I'm really starting to think this isn't possible.
  • whoknows
    whoknows over 6 years
    That seems to just show all the test cases of child suites, which isn't what I'm going for. In this case, the query sweet will have no child suites, but query one or more static suites and the query based suite should contain all the test cases of the test suites included in the query.
  • whoknows
    whoknows over 6 years
    I think you're right. We currently do a similar workaround. In our setup, instead of tagging the test case with the name (we use the tags for something else), we create a link between the static suite and the test case being added. Then we can query those suites and the children of them will be the test cases. I will look into the automated creation via web hooks though, that might be better than our current automated solution. Thanks for the idea!