Click here to Skip to main content
15,881,588 members
Articles / All Topics

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

Rate me:
Please Sign up or sign in to vote.
4.91/5 (3 votes)
30 Jul 2010Ms-PL 47.1K   3   9
In this post, I’ll show how to get all Test Suites hierarchy Test Cases and more.

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, let's 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 a QA manager, it will be awesome to get a view of all Team Projects and all Test Plans under one window.

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 are located under - c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\

Step 2: Connect to Team Foundation Server

C#
private TfsTeamProjectCollection _tfs;
private ITestManagementTeamProject _testproject;
C#
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

C#
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

C#
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

C#
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);
    }
}

image

Picture - B
This article was originally posted at http://feeds.feedburner.com/ShaiRaiten

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Architect Sela
Israel Israel
Shai Raiten is VS ALM MVP, currently working for Sela Group as a ALM senior consultant and trainer specializes in Microsoft technologies especially Team System and .NET technology. He is currently consulting in various enterprises in Israel, planning and analysis Load and performance problems using Team System, building Team System customizations and adjusts ALM processes for enterprises. Shai is known as one of the top Team System experts in Israel. He conducts lectures and workshops for developers\QA and enterprises who want to specialize in Team System.

My Blog: http://blogs.microsoft.co.il/blogs/shair/

Comments and Discussions

 
QuestionHelps alot :) Pin
roneytyc15-Oct-17 16:42
roneytyc15-Oct-17 16:42 
QuestionRegarding add testcase to Requirement test suite. Pin
shamshaTest16-Jan-13 18:28
shamshaTest16-Jan-13 18:28 
AnswerRe: Regarding add testcase to Requirement test suite. Pin
Shai Raiten20-Jan-13 1:45
Shai Raiten20-Jan-13 1:45 
QuestionError on plan.RootSuite.Entries.Count Pin
SainathAdhiraj26-Nov-12 20:53
SainathAdhiraj26-Nov-12 20:53 
GeneralIs there a way to get the child suites in the same order that they are displayed in MTM? Pin
Todd Andrew9-May-11 11:29
Todd Andrew9-May-11 11:29 
GeneralRe: Is there a way to get the child suites in the same order that they are displayed in MTM? Pin
Shai Raiten9-May-11 20:17
Shai Raiten9-May-11 20:17 
GeneralRe: Is there a way to get the child suites in the same order that they are displayed in MTM? Pin
Todd Andrew10-May-11 4:21
Todd Andrew10-May-11 4:21 
GeneralLooks good... Pin
Sandeep Mewara30-Jul-10 22:09
mveSandeep Mewara30-Jul-10 22:09 
GeneralRe: Looks good... Pin
Shai Raiten30-Jul-10 22:17
Shai Raiten30-Jul-10 22:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.