How to get unit test results using TFS Rest API?

前端 未结 2 1282
礼貌的吻别
礼貌的吻别 2020-12-10 23:42

How to retrieve the unit test results of a build in TFS using Rest API?

The build definition uses VNext (Visual Studio 2015 Update 3).

var vssConnect         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-11 00:00

    The test result of the build is stored in test runs, so you need to get the test run of the build first and then retrieve the test result from the test run. Following is the code sample:

    class Program
    {
        static void Main(string[] args)
        {
            string ur = "https://xxxxxxx/";
            TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(ur));
            //Get build information
            BuildHttpClient bhc = ttpc.GetClient();
            string projectname = "Project";
            int buildId = 1;
            Build bui = bhc.GetBuildAsync(projectname,buildId).Result;
            //Get test run for the build
            TestManagementHttpClient ithc = ttpc.GetClient();
    
            Console.WriteLine(bui.BuildNumber);
    
            QueryModel qm = new QueryModel("Select * From TestRun Where BuildNumber Contains '" + bui.BuildNumber + "'");
    
            List testruns = ithc.GetTestRunsByQueryAsync(qm,projectname).Result;
            foreach (TestRun testrun in testruns)
            {
    
                List testresults = ithc.GetTestResultsAsync(projectname, testrun.Id).Result;
                foreach (TestCaseResult tcr in testresults)
                    {
                        Console.WriteLine(tcr.TestCase.Name);
                        Console.WriteLine(tcr.Outcome);
                    }
    
                Console.ReadLine();
            }
            Console.ReadLine();
        }
    }
    

提交回复
热议问题