【发布时间】:2016-10-16 17:35:20
【问题描述】:
我正在使用 TFS API 添加测试运行,并希望将多个测试点添加到测试运行中,并将一个测试结果添加到测试运行中的每个测试点。当我在添加第二个测试点后尝试检索测试结果时,我只能得到一个测试结果(对应于第一个测试点的那个)。
我在 Windows 7 上的 Visual StudioEnterprise 2015 中使用 C# 4.5.2 我的代码是:
设置测试运行(我在测试开始时运行一次):
TfsConfigurationServer configurationServer =
TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);
CatalogNode collectionNode = configurationServer.CatalogNode.QueryChildren(
new[] { CatalogResourceTypes.ProjectCollection },
false, CatalogQueryOptions.None).Single();
Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
TfsTeamProjectCollection teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId);
ITestManagementService testManagementService = teamProjectCollection.GetService<ITestManagementService>();
ITestManagementTeamProject testProject = testManagementService.GetTeamProject(teamProjectName);
ITestPlan testPlan = testProject.TestPlans.Find(TestPlanId);
ITestRun testRun = testPlan.CreateTestRun(true);
testRun.DateStarted = DateTime.Now;
testRun.IsAutomated = true;
testRun.Title = "Automated test run " + testRun.DateStarted.ToString();
testRun.State = TestRunState.InProgress;
将测试结果添加到测试运行(我在每个测试场景完成后运行):
public void AddTestResult(int testCaseId, string testResult,DateTime startedTime, DateTime endedTime, ITestRun testRun)
{
if (testRun == null)
{
CreateTestRun();
}
TfsConfigurationServer configurationServer =
TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);
ReadOnlyCollection<CatalogNode> collectionNodes = configurationServer.CatalogNode.QueryChildren(
new[] { CatalogResourceTypes.ProjectCollection },
false, CatalogQueryOptions.None);
var collectionNode = collectionNodes.Single();
// List the team project collections
// Use the InstanceId property to get the team project collection
Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
TfsTeamProjectCollection teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId);
ITestManagementService testManagementService = teamProjectCollection.GetService<ITestManagementService>();
ITestManagementTeamProject testProject = testManagementService.GetTeamProject(teamProjectName);
ITestPlan testPlan = testProject.TestPlans.Find(TestPlanId);
var testPoints = testPlan.QueryTestPoints("SELECT * FROM TestPoint WHERE TestCaseID = '" + testCaseId + "'");
var testPoint = testPoints.First();
testRun.AddTestPoint(testPoint,null);
testRun.TestEnvironmentId = testPlan.AutomatedTestEnvironmentId;
testRun.Save();
var tfsTestResult = testRun.QueryResults().Single(r=>r.TestPointId==testPoint.Id);
tfsTestResult.State = TestResultState.Completed;
tfsTestResult.DateCompleted = endedTime;
tfsTestResult.DateStarted = startedTime;
tfsTestResult.Duration = endedTime - startedTime;
if (testResult == "passed" && tfsTestResult.Outcome!=TestOutcome.Failed)
{ // ^ if multiple specflow scenarios have been run with the same test case ID then don't set it to pass if a previous one in this test run has failed
tfsTestResult.Outcome = TestOutcome.Passed;
}
else
{
tfsTestResult.Outcome = TestOutcome.Failed;
}
tfsTestResult.Save();
testRun.Save();
}
对于第一个场景,它工作得很好,但是在下一个场景使用不同的 testCaseId 之后,它会在尝试找到与该测试点对应的测试结果时抛出异常(测试结果查询只返回一个对应于的测试结果我第一次运行该方法时添加的第一个测试点)。
当我使用第二个不同的 ID 运行方法时,这是引发异常的行:
var tfsTestResult = testRun.QueryResults().Single(r=>r.TestPointId==testPoint.Id);
如果我使用与第一次相同的 ID 再次运行该方法。
例外是:
“System.InvalidOperationException”类型的异常发生在 System.Core.dll 但未在用户代码中处理
附加信息:序列不包含匹配元素
如果没有匹配的测试结果,我尝试跳过更新测试结果的位,并且看起来在 MTM 中没有添加第二个测试点,所以我猜这是相关的。
【问题讨论】:
-
你遇到了什么异常?
-
@Cece-MSFT 我已经用异常消息更新了问题。
-
您是否在 MTM 中添加了多个配置?您可以查看这篇关于如何使用 MTM api 的文章:blogs.msdn.microsoft.com/densto/2010/03/03/…
-
@Cece-MSFT 我找到了答案,无法按照我想要的方式进行,保存测试运行后无法添加测试点。您可以通过添加所有测试点然后保存来解决此问题。