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 image](/KB/cs/97699/image_thumb_73FE0131-r-700.png)
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 image](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
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 image](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Code Example
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;
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;
IStaticTestSuite subSuite = TestHelper.FindSuite(collection, area);
if (subSuite.Id == 0)
{
subSuite.Title = area;
suite.Entries.Add(subSuite);
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).
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 4](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
![Image 5](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
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/