Click here to Skip to main content
15,881,803 members
Articles / DevOps / TFS

TFS API Part 28 - Test Suite Hierarchy Builder

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
30 Jul 2010Ms-PL2 min read 24.4K   5  
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 does not feel 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 uses 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 a full hierarchy in MTM based on the structure of the Areas in your 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

Code Example

C#
/// <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 a 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).

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

Image 4Image 5

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

 
-- There are no messages in this forum --