1

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 vssConnection = new VssConnection(_configurationSpec.TeamProjectCollection, 
    new VssClientCredentials());
_buildClient = vssConnection.GetClient<BuildHttpClient>();
Community
  • 1
  • 1
codelab
  • 406
  • 3
  • 14
  • It looks like you can get the logs of a particular build (https://www.visualstudio.com/en-us/docs/integrate/api/build/builds#logs) but just based on the documentation, doesn't look like you can get just the test results yet :( – m00nbeam360.0 Sep 07 '16 at 17:44

3 Answers3

5

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();
    }
}
Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60
  • Which reference's do i need? i can't find those, is it some extra package i have to install first to use this? i work with vs 2015 – Dom84 Feb 28 '17 at 08:00
  • @Dom84 Install the nuget packages mentioned in this site: https://www.visualstudio.com/en-us/docs/integrate/get-started/client-libraries/dotnet – Eddie Chen - MSFT Feb 28 '17 at 08:06
1

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..." enter image description here

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
  • 1
    Any idea why would API return this error: {"$id":"1","innerException":null,"message":"This API is only applicable to logs stored in the file container.","typeName":"Microsoft.TeamFoundation.Build.WebApi.InvalidLogLocationException, Microsoft.TeamFoundation.Build2.WebApi, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a","typeKey":"InvalidLogLocationException","errorCode":0,"eventId":3000} – ShP Aug 17 '17 at 19:00
  • @ShP This works well on my side, you will receive a log summary with each build step in the build definition. And if you want to get detail logs of each step, you need to add a /logID after `/logs` before `?api-version=2.0`. Seems your issue has been solved here: https://stackoverflow.com/questions/45743037/tfs-rest-api-not-working-for-logs – PatrickLu-MSFT Aug 18 '17 at 01:36
  • Kind of, I am able to get zip, but still getting error when try to get json response – ShP Aug 18 '17 at 01:41
0

If you are trying to fetch tests for a build from Azure DevOps you can use the new method available in the Microsoft.TeamFoundationServer.Client nuget package:

// Get Test Management client
using var testMgmtClient = connection.GetClient<TestManagementHttpClient>();

// Get tests run for a certain build
var tests = testMgmtClient.GetTestRunsAsync(projectName, builds[0].Uri.AbsoluteUri, includeRunDetails: true).Result;
Viktor
  • 679
  • 6
  • 21