TFS API Part 28 – Test Suite Hierarchy Builder

26/07/2010

TFS API Part 28 -Test Suite Hierarchy Builder

In my previous post regarding TFS API TFS API Part 27 – Test Plans, Test Suites, Test Cases Mapping, I showed how to create and obtain Test Plans, Test Suite and Test Cases objects.

As part of Quality Center to TFS 2010 Migration Tool I’m translating Quality Center hierarchy into Areas in TFS 2010 –

as you can see from the following pictures the QC hierarchy(middle picture) looks the same as TFS Areas (left picture) but it’s not feels the same…
Feels the same? when using QC you can navigate using the Tree View and drill down to the Test Suite (Folder) you want, to accomplish those actions in TFS you will have to write a Query and each time change the query values, Ammm… it’s not a good solution – 
But using Microsoft Test Manager you can create Test Suites and Requirements with the same hierarchy as QC. (right picture)

image

In order to create those hierarchies quickly in TFS I built a tool just for that, this tool is using the following articles to complete this task.

The tool is very simple – Connect to TFS 2010, Create or Select Test Plan, Select Areas (One or more) and click start.

This action will take couple of minutes and in the end you will have full hierarchy in MTM based on the structure of the Areas in you project and the Test Cases assigned under each Area.

image 

The result will be inside the Test Plan you picked before and all the Test Suite will be based on the Area Paths in the current Team Project.

Under each Test Suite you should see the related Test Cases.

image

Download Tool

Code Example:

Download Source Code

/// <summary>
/// Get's a full path of Area, split it and for each part create Test Suite and apply the Test Cases beneath it.
/// </summary>
/// <param name="full_area">Area Path, for example - CMMI\First Area\Sub Area\Content Area</param>
void CreateTestSuite(string full_area)
{
    try
    {
        string[] areas = full_area.Split('\\');
        string full_path = string.Empty;
        IStaticTestSuite suite = null;
        string current_area = string.Empty;

        for (int i = 0; i < areas.Length; i++)
        {
            if (!string.IsNullOrEmpty(areas[i]))
            {
                string area = areas[i].RemoveBadChars();
                current_area += area;

                //The first item, find it and assigned to suite object.
                if (i == 1)
                {
                    ITestSuiteEntryCollection collection = _plan.RootSuite.Entries;
                    suite = TestHelper.FindSuite(collection, area);
                    if (suite.Id == 0)
                    {
                        suite.Title = area;
                        TestHelper.AddTests(suite, current_area);
                        _plan.RootSuite.Entries.Add(suite);
                    }
                }
                else
                {
                    ITestSuiteEntryCollection collection = suite.Entries;
                    //* collection - Perform search only under the suite.Entries  - Duplicate items allowed.
                    IStaticTestSuite subSuite = TestHelper.FindSuite(collection, area);

                    if (subSuite.Id == 0)
                    {//Cannot find Test Suite
                        subSuite.Title = area;
                        suite.Entries.Add(subSuite);
                        //After creating the Test Suite - Add the related TestCases based on the Area Path.
                        TestHelper.AddTests(subSuite, current_area);
                    }

                    suite = subSuite;
                }
                current_area += "\\";
                _plan.Save();
            }
        }
        _plan.Save();
    }
    catch (TestSuiteInvalidOperationException testex)
    {
        if (!testex.Message.Contains("Duplicate suite name detected"))
            throw new ArgumentNullException(testex.Message);
    }
}

TestSuiteHelper contains couple of simple actions like AddTests – that perform a Query in TFS to find all Test Cases under specific area path, also FindSuite as a recursive search under each suite (Recursive search because Suite doesn’t have unique names so this should be individual search for each Suite).

public class TestSuiteHelper

{

    private ITestManagementTeamProject _testproject;

    private Project _project;

    public TestSuiteHelper(ITestManagementTeamProject TestManagementTeamProject, Project project)

    {

        this._testproject = TestManagementTeamProject;

        this._project = project;

    }

    public void AddTests(IStaticTestSuite suite, string area)

    {

        IEnumerable<ITestCase> testcases = _testproject.TestCases.Query(string.Format("Select * from [WorkItems] where [System.AreaPath] = \"{0}\\{1}\"", _project.Name, area));

        foreach (ITestCase testcase in testcases)

        {

            suite.Entries.Add(testcase);

        }

    }

    public IStaticTestSuite FindSuite(ITestSuiteEntryCollection collection, string title)

    {

        foreach (ITestSuiteEntry entry in collection)

        {

            IStaticTestSuite suite = entry.TestSuite as IStaticTestSuite;

            if (suite != null)

            {

                if (suite.Title == title)

                    return suite;

                else if (suite.Entries.Count > 0)

                    FindSuite(suite.Entries, title);

            }

        }

        return _testproject.TestSuites.CreateStatic();

    }

}

Download Tool

Download Source Code

Add comment
facebook linkedin twitter email

Leave a Reply

one comment

  1. Pavel20/10/2010 ב 23:43

    hi,
    how I can change the order of test cases in test suite using API?
    Pavel