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).
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.
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.
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;
//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).
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();
}
}