How to get unit test results using TFS Rest API?

五迷三道 提交于 2019-11-29 08:51:24

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<BuildHttpClient>();
        string projectname = "Project";
        int buildId = 1;
        Build bui = bhc.GetBuildAsync(projectname,buildId).Result;
        //Get test run for the build
        TestManagementHttpClient ithc = ttpc.GetClient<TestManagementHttpClient>();

        Console.WriteLine(bui.BuildNumber);

        QueryModel qm = new QueryModel("Select * From TestRun Where BuildNumber Contains '" + bui.BuildNumber + "'");

        List<TestRun> testruns = ithc.GetTestRunsByQueryAsync(qm,projectname).Result;
        foreach (TestRun testrun in testruns)
        {

            List<TestCaseResult> 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();
    }
}

You can try to get the logs of the related step by using this Rest API in a powershell script.

GET https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/Fabrikam-Fiber-Git/_apis/build/builds/391/logs?api-version=2.0

It will return logs1, logs2 corresponds step1, step2.

{
  "count": 4,
  "value": [
    {
      "lineCount": 3,
      "createdOn": "2015-07-16T19:53:19.747Z",
      "lastChangedOn": "2015-07-16T19:53:19.92Z",
      "id": 1,
      "type": "Container",
      "url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c/_apis/build/builds/391/logs/1"
    },
    {
      "lineCount": 113,
      "createdOn": "2015-07-16T19:53:29.387Z",
      "lastChangedOn": "2015-07-16T19:53:29.44Z",
      "id": 2,
      "type": "Container",
      "url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c/_apis/build/builds/391/logs/2"
    },

For example:

Just need to get the log of step 4 "Test Assemblies..."

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!