如何通过配置更新TFS测试结果?

时间:2016-10-21 21:05:01

标签: tfs mtm

我有一个方法似乎只是为了运行它的配置更新TFS测试结果。但它看起来效率很低。有人能帮助我弄清楚我做错了什么吗? - 特别是我似乎不应该为给定的测试ID获取testSuite,然后遍历所有测试,以便将这些套件与我已有的测试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();
        }
    }

1 个答案:

答案 0 :(得分:0)

为了获得测试结果,您可以通过测试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的链接,可以帮助您参考: