【问题标题】:how do I update TFS test results by configuration?如何通过配置更新 TFS 测试结果?
【发布时间】:2016-10-25 08:37:31
【问题描述】:

我有一种方法似乎可以仅针对其运行的配置更新 TFS 测试结果。但这似乎非常低效。有人可以帮我弄清楚我做错了什么吗? - 具体来说,我似乎不必为给定的测试 ID 获取 testSuites,然后遍历这些套件的所有测试以与我已有的测试 ID 进行比较。

例如,我针对 Windows 8/Chrome 进行了测试。所以 TestContext 包含映射到配置的适当值。我想在它存在的每个测试套件中更新该配置的测试结果。这是代码:

    public void UpdateResult(TestContext context, bool passed)
    {
        int testCaseID = Convert.ToInt32(context.Test.Properties["id"]);

        TfsTeamProjectCollection projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsUri);

        ITestManagementService service = projectCollection.GetService<ITestManagementService>();
        ITestManagementTeamProject teamProject = service.GetTeamProject(teamProjectName);

        ITestCase testCase = teamProject.TestCases.Find(testCaseID);
        ITestSuiteCollection testSuites = teamProject.TestSuites.ReferencingTestCase(testCaseID);

        if (testCase != null)
        {
            // ---- Get Test Plan
            int testPlanId = 20589;
            ITestPlan testPlan = teamProject.TestPlans.Find(testPlanId);

            // ---- Create Test Run
            ITestRun testRun = teamProject.TestRuns.Create();
            testRun = testPlan.CreateTestRun(true);

            // ---- Get TestPoint
            ITestPointCollection points = testPlan.QueryTestPoints("SELECT * FROM TestPoint WHERE TestCaseId =" + testCaseID);
            foreach (ITestPoint tp in points)
            {
                //Get configuration, which contains configuration values
                ITestConfiguration testConfiguration = teamProject.TestConfigurations.Query("Select * from TestConfiguration WHERE Name='" + tp.ConfigurationName + "'")[0];
                IDictionary<string, string> testConfigValues = testConfiguration.Values;

                if ((!testConfigValues.ContainsKey("Browser") ||
                    testConfigValues["Browser"].ToLower() == context.Test.Properties["Browser"].ToString().ToLower())
                    && (!testConfigValues.ContainsKey("Operating System") ||
                    testConfigValues["Operating System"].ToLower() == context.Test.Properties["Operating System"].ToString().ToLower())
                   )
                {
                    testRun.AddTestPoint(tp, testPlan.Owner);
                }

            }

            testRun.State = TestRunState.Completed;
            testRun.Save();

            foreach (ITestSuiteBase testSuite in testSuites)
            {
                foreach (ITestSuiteEntry testCse in testSuite.TestCases)
                {
                    ITestCaseResultCollection results = testRun.QueryResults();
                    if (testCse.TestCase.Id == testCaseID)
                    {

                        foreach (IdAndName config in testCse.Configurations)
                        {
                            IEnumerable<ITestCaseResult> testCaseResults = from tr in results
                                                                           where
                                                                           tr.TestConfigurationId == config.Id &&
                                                                           tr.TestCaseId == testCase.Id
                                                                           select tr;

                            foreach (ITestCaseResult result in testCaseResults)
                            {
                                result.Outcome = passed ? TestOutcome.Passed : TestOutcome.Failed;
                                result.State = TestResultState.Completed;
                                result.Save(true);
                            }
                        }
                    }


                }
            }

            testRun.Save();
            testRun.Refresh();
        }
    }

【问题讨论】:

    标签: tfs mtm


    【解决方案1】:

    要获取测试结果,您可以通过测试ID直接获取测试结果。例如:

    TfsTeamProjectCollection teamCollection;
                ITestManagementService service;
                ITestManagementTeamProject project;
                var picker = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false);
                picker.ShowDialog();
                if (picker.SelectedTeamProjectCollection != null && picker.SelectedProjects != null)
                {
                    teamCollection = picker.SelectedTeamProjectCollection;
                    service = teamCollection.GetService<ITestManagementService>();
                    project = service.GetTeamProject(picker.SelectedProjects.First().Name);
                }
                else
                {
                    return;
                }
    
    //Get Test result
     var testResults = project.TestResults.ByTestId([test case id]);
    
     // iterate each result for the case
     foreach (ITestCaseResult result in testResults)
     {
         //TODO other code
         //update result
         result.Outcome = TestOutcome.Failed;
         result.Save(true);
    }
    

    另外添加一些相关TFS API的链接,可以帮助你参考:

    【讨论】:

    • 因为这必须是程序化的,所以选择器对我没有多大好处。所以我假设有用的部分是通过TestId获取testResults。这很有帮助,很高兴知道,但它不能解决我不认为的问题?我的问题是更新所有配置,除非我做了我发布的那个怪物。如何在您的代码中合并仅更新选择配置?另外,我不需要调用 AddTestPoint 吗?
    • 您有更多信息可以帮助我发表评论吗?我看过您提供的那些链接 - 都没有谈论您如何将配置合并到更新中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2019-06-23
    • 2013-04-12
    • 1970-01-01
    • 2011-11-08
    • 2021-05-20
    相关资源
    最近更新 更多