TFS API 第 28 部分 - 测试套件层级构建器





5.00/5 (3投票s)
测试套件层级构建器
在我的上一篇关于 TFS API 的文章 TFS API 第 27 部分 – 测试计划、测试套件、测试用例映射 中,我展示了如何创建和获取测试计划、测试套件和测试用例对象。
作为 Quality Center 到 TFS 2010 迁移工具 的一部分, 我正在将 Quality Center 的层级结构转换为 TFS 2010 中的区域。
正如以下图片所示,QC 的层级结构(中间图片)看起来与 TFS 区域(左侧图片)相同,但感觉却不一样…
感觉一样吗? 在使用 QC 时,你可以使用树形视图导航并钻取到你想要的测试套件(文件夹),为了在 TFS 中完成这些操作,你必须编写查询并每次更改查询值,嗯… 这不是一个好的解决方案 –
但是使用 Microsoft 测试管理器,你可以创建与 QC 具有相同层级的测试套件和需求(右侧图片)。
为了在 TFS 中快速创建这些层级结构,我为此构建了一个工具。该工具使用以下文章来完成此任务
该工具非常简单 – 连接到 TFS 2010,创建或选择测试计划,选择区域(一个或多个),然后单击开始。
此操作需要几分钟,最终你将在 MTM 中获得一个完整的层级结构,该结构基于项目中的区域结构以及分配在每个区域下的测试用例。
结果将在你之前选择的测试计划中,并且所有测试套件都将基于当前团队项目的区域路径。
在每个测试套件下,你应该会看到相关的测试用例。
代码示例
/// <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
包含一些简单的操作,例如 AddTests - 它在 TFS 中执行查询以查找特定区域路径下的所有测试用例,以及 FindSuite 作为每个套件下的递归搜索(递归搜索是因为套件没有唯一的名称,因此应该对每个套件进行单独搜索)。
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();
}
}