TFS API将多个测试点和结果添加到一个测试运行中

时间:2016-06-15 14:24:43

标签: c# tfs mtm

我正在使用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再次运行该方法。 例外是:

  

类型&#39; System.InvalidOperationException&#39;的例外情况发生在   System.Core.dll但未在用户代码中处理

     

附加信息:序列不包含匹配元素

我尝试跳过更新测试结果的位,如果没有匹配的测试结果,并且看起来在MTM中第二个测试点没有被添加,所以我猜这是相关的。

1 个答案:

答案 0 :(得分:3)

我终于发现我目前无法做到的事情。保存TestRun后,无法再添加TestPoint条目。 将多个测试点添加到测试运行的唯一方法是在保存测试运行之前将它们全部添加。 参考: TFS API: Cannot add testpoint after testrun is saved

https://blogs.msdn.microsoft.com/nidhithakur/2011/04/08/tfs-programatically-importing-testcase-results-to-mtm/

  

理想情况下,您应该能够在添加测试时添加结果   指向运行但run.Save()API仅适用于单个   马上保存。因此,您需要添加所有测试点,保存   测试运行,然后遍历运行集合以添加结果   单独

我已经将代码更改为在运行期间存储测试结果,然后批量添加到新的TestRun并在所有测试完成后保存TestRun。 我的新代码有效:

    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(investigateRelease1TestPlanId);
    foreach (MtmTestResultInfo result in testResults)
    {
        var testPoints = testPlan.QueryTestPoints("SELECT * FROM TestPoint WHERE TestCaseID = '" + result.TestCaseId + "'");
        var testPoint = testPoints.First();
        testRun.AddTestPoint(testPoint, null);
    }

    testRun.DateStarted = dateStarted;
    testRun.DateCompleted = dateCompleted;
    TimeSpan timeTaken = dateCompleted - dateStarted;
    testRun.State = TestRunState.Completed;
    testRun.Save();
    //cannot add comment until after test run is saved
    testRun.Comment = "my comment"

    var tfsTestResults = testRun.QueryResults();
    foreach (MtmTestResultInfo result in testResults)
    {
        ITestCaseResult tfsTestResult = tfsTestResults.Single(r => r.TestCaseId == result.TestCaseId);
        tfsTestResult.DateStarted = result.DateStarted;
        tfsTestResult.DateCompleted = result.DateCompleted;
        tfsTestResult.Outcome = result.Outcome;
        tfsTestResult.Comment = result.Comment;
        tfsTestResult.ErrorMessage = result.ErrorMessage;
        tfsTestResult.RunBy = testRun.Owner;
        tfsTestResult.Duration = result.DateCompleted - result.DateStarted;
        tfsTestResult.State=TestResultState.Completed;

        tfsTestResult.Save();
        testRun.Save();
    }

    testRun.Save();

支持类来存储测试结果:

public class MtmTestResultInfo
{
    public DateTime DateStarted { get; set; }
    public DateTime DateCompleted { get; set; }
    public TestOutcome Outcome { get; set; }
    public int TestCaseId { get; set; }
    public string Comment { get; set; }
    public string ErrorMessage { get; set; }

}
相关问题