TFS API 将多个测试点和结果添加到一次测试运行中 [英] TFS API Add multiple test points and results to one test run

查看:25
本文介绍了TFS API 将多个测试点和结果添加到一次测试运行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TFS API 添加一个测试运行,并希望向一个测试运行添加多个测试点,并在测试运行中为每个测试点添加一个测试结果.当我在添加第二个测试点后尝试检索测试结果时,我只得到一个测试结果(对应于第一个测试点的那个).

I am using the TFS API to add a test run and would like to add multiple test points to a test run and add one test result to each test point in the test run. When I try to retrieve the test results after adding the second test point I only get one test result back (the one that corresponds to the first test point).

我在 Windows 7 上的 Visual StudioEnterprise 2015 中使用 C# 4.5.2 我的代码是:

I am using C# 4.5.2 in Visual StudioEnterprise 2015 on Windows 7 my code is:

设置测试运行(我在测试开始时运行了一次):

setup test run (I run this once at the beginning of my tests):

    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;

将测试结果添加到测试运行(我在每个测试场景完成后运行):

Add test result to test run (I run this after each test scenario finishes):

    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 的场景之后,它会在尝试找到该测试点的相应测试结果时抛出异常(测试结果查询仅返回一个对应于的测试结果)第一次运行该方法时添加的第一个测试点).

For the first scenario it works perfectly well, but after the next scenario with a different testCaseId it throws an exception when trying to find the corresponding test result to that test point (the test results query only returns one test result that corresponds to the first test point that I added the first time I run the method).

这是当我使用第二个不同的 ID 运行方法时引发异常的行:var tfsTestResult = testRun.QueryResults().Single(r=>r.TestPointId==testPoint.Id);如果我使用与第一次运行时相同的 ID 再次运行该方法.例外是:

This is the line that throws the exception when I run the method with a second, different ID: var tfsTestResult = testRun.QueryResults().Single(r=>r.TestPointId==testPoint.Id); If I run the method again with the same Id as the first time it works. The exception is:

System.InvalidOperationException"类型的异常发生在System.Core.dll 但未在用户代码中处理

An exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code

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

Additional information: Sequence contains no matching element

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

I tried skipping the bit that updates the test result if there's no matching test result and it looks like in MTM the second test point isn't being added, so I guess this is related.

推荐答案

我终于发现我试图做的事情目前是不可能的.保存 TestRun 后,您将无法再添加任何 TestPoint 条目.将多个测试点添加到测试运行的唯一方法是在保存测试运行之前将它们全部添加.参考:TFS API:保存测试运行后无法添加测试点

I finally found out that what I was trying to do isn't currently possible. Once you save a TestRun you cannot add any more TestPoint entries. The only way to add multiple test points to a test run is to add them all before you save the test run. Reference: 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 仅适用于单个立即保存.所以,你需要添加所有的测试点,保存测试运行,然后迭代运行集合以添加结果个别.

Ideally, you should be able to add the results while adding the test point to the run but the run.Save() API is only working for single Save right now. So, you will need to add all the testpoints, save the test run and then iterate over the run collection to add results individually.

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

I've changed my code to store test results during the run, then batch add them to a new TestRun and save the TestRun after all tests have finished. My new code, which works is:

    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();

支持类来存储测试结果:

Supporting class to store test results:

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; }

}

这篇关于TFS API 将多个测试点和结果添加到一次测试运行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆