TFS API Part 27 – Test Plans, Test Suites, Test Cases Mapping

06/07/2010

TFS API Part 27 – Test Plans, Test Suites, Test Cases Mapping

In my last post TFS API Part 26 – Add/Remove Test Plans I showed how to Add remove Test Plan using MTM API.image                    Picture – A

Now when we know how to use work with Test Plan lets deep dive to all items under each Test Plan, in this post I’ll show how to get all Test Suites hierarchy Test Cases and more.

As QA manager it will be awesome to get a view of all Team Project and all Test Plans under one window.

Download Demo Project


Step 1: Create Project and Add Reference

Create an WPF/WinForm application and add the following references:

  • Microsoft.TeamFoundation.WorkItemTracking.Client.dll
  • Microsoft.TeamFoundation.Client.dll
  • Microsoft.TeamFoundation.dll
  • Microsoft.TeamFoundation.TestManagement.Client

All files located under – c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\

Step 2: Connect to Team Foundation Server

private TfsTeamProjectCollection _tfs;
private ITestManagementTeamProject _testproject;
TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false);
tpp.ShowDialog();

if (tpp.SelectedTeamProjectCollection != null)
{
    this._tfs = tpp.SelectedTeamProjectCollection;
    ITestManagementService test_service = (ITestManagementService)_tfs.GetService(typeof(ITestManagementService));
    this._testproject = test_service.GetTeamProject(tpp.SelectedProjects[0].Name);

    GetTestPlans();

    btn_add.IsEnabled = true;
    btn_remove_plan.IsEnabled = true;
}

Step 3: Get Test Plans

void GetTestPlans(ITestManagementTeamProject testproject)
    {
        ITestPlanCollection plans = testproject.TestPlans.Query("Select * From TestPlan");

        TreeViewItem root = null;
        root = new TreeViewItem();
        root.Header = ImageHelpers.CreateHeader(testproject.WitProject.Name, ItemTypes.TeamProject);
        TreeMain.Items.Add(root);

        foreach (ITestPlan plan in plans)
        {
            TreeViewItem plan_tree = new TreeViewItem();
            plan_tree.Header = ImageHelpers.CreateHeader(plan.Name, ItemTypes.TestPlan);

            if (plan.RootSuite != null && plan.RootSuite.Entries.Count > 0)
                GetPlanSuites(plan.RootSuite.Entries, plan_tree);

            root.Items.Add(plan_tree);
        }
    }

Step 4:Get Test Suites

void GetPlanSuites(ITestSuiteEntryCollection suites, TreeViewItem tree_item)
{
    foreach (ITestSuiteEntry suite_entry in suites)
    {
        IStaticTestSuite suite = suite_entry.TestSuite as IStaticTestSuite;
        if (suite != null)
        {
            TreeViewItem suite_tree = new TreeViewItem();
            suite_tree.Header = ImageHelpers.CreateHeader(suite.Title, ItemTypes.TestSuite);

            GetTestCases(suite, suite_tree);

            tree_item.Items.Add(suite_tree);

            if (suite.Entries.Count > 0)
                GetPlanSuites(suite.Entries, suite_tree);

        }
    }
}

Step 5: Get Test Cases                                                                                                      

void GetTestCases(IStaticTestSuite suite, TreeViewItem tree_item)
{
    //AllTestCases - Will show all the Test Cases under that Suite even in sub suites.
    //ITestCaseCollection testcases = suite.AllTestCases; – See Picture B

    //Will bring only the Test Case under a specific Test Suite. – See Picture A
    ITestSuiteEntryCollection suiteentrys = suite.TestCases;

    foreach (ITestSuiteEntry testcase in suiteentrys)
    {
        TreeViewItem test = new TreeViewItem();
        test.Header = ImageHelpers.CreateHeader(testcase.Title, ItemTypes.TestCase);
        tree_item.Items.Add(test);
    }
}

Picture – B

image

Download Demo Project

Add comment
facebook linkedin twitter email

Leave a Reply

6 comments

  1. Bertrand23/09/2010 ב 23:52

    Not spam.
    This is great help. Thanks.
    Great to be able to see full overview in a collection

  2. jimmy01/06/2011 ב 15:43

    Hi,

    This will be really great of you if you can help me out that how I can use TFS sdk for java to export my testcases in testmanager from an xml file.

    This is really important for me as I have a task to export testcases from xml file to testmanager using java sdk for tfs.

    Thank you very mucch.

    Regards,

    Jimmy

  3. jimmy06/06/2011 ב 14:43

    Hi,

    When I try to connect to TFS using java sdk the following erro occurs:

    ERROR [main] PlatformUtilsProxy – Proxy ‘SecureStorage.defaultProxy’ ran out of implementations to use

    Can you please guide me that what I am doing wrong here?

    Following is my code.

    public class TestManagerExport {
    static boolean b=true;
    public static void main(String args[]){

    TFSTeamProjectCollection tpc =

    new TFSTeamProjectCollection(“http://212.114.71.52:8080/DefaultCollection”,”chriko”,”domain”,”P2ssw0rd”);
    b=tpc.hasAuthenticated();
    System.out.println(b);
    }
    }

    Thanks in advance.

    Jimmy

  4. Eirik A01/07/2011 ב 03:15

    Hi Shai,

    These have been super helpful, thank you!

    I have a pair of questions for you:

    1. There is a reasonable amount of documentation regarding queries against the WorkItems table, but I haven’t found much regarding the schema of the TestPlan, TestSuite tables. Do you know where I could find that?

    2. I tried the following snippet:

    ITestManagementTeamProject proj = GetProject(serverurl, project);

    ITestSuiteCollection suiteCollection = proj.TestSuites.Query(“select * from TestSuite where ParentId<>0″);

    foreach (var suite in suiteCollection)
    {
    Console.WriteLine(“Title: ” + suite.Title);
    Console.WriteLine(suite.Parent != null ? “Parent: ” + suite.Parent.Title : “No Parent found” );
    }

    None of the suite’s parents were filled in. They were all null. However, I can modify the query based on ‘ParentId <> 0′ so I know some of them have parents.

    What am I doing wrong?

    Thanks again

  5. Alex Parinov27/03/2012 ב 09:49

    Hi Shai,
    It looks Like you the only person in the Internet who knows API of TFS. Thanks! I have small question. I`m trying to add test case to test suite but always get an error: [Microsoft.TeamFoundation.TestManagement.Client.TestObjectNotFoundException] = {“Test suite 0 not found.”}

    Here is the code:
    public void CopyTestCases(IStaticTestSuite Source_suite, IStaticTestSuite Destination_suite, ITestManagementTeamProject destination_testproject)
    {
    if (Source_suite.TestCases != null)
    if (Source_suite.TestCases.Count > 0)
    {
    try
    {
    foreach (ITestSuiteEntry testcase in Source_suite.TestCases)
    {
    ITestCase new_testCase = destination_testproject.TestCases.Create();
    new_testCase.Title = testcase.Title;
    //new_testCase.
    new_testCase.Save();
    Destination_suite.Entries.Add(new_testCase);
    }
    }
    catch (Exception e)
    {
    MessageBox.Show(e.Message);
    }
    }
    }