Jteve_Sobs
Jteve_Sobs

Reputation: 23

How to get test results from TFS by Release ID in C#

I was unable to find a way to get the test results of a given release in C#.

What I did before was, getting the test results by the build ID.

Here's a sample code

  int buildId = 123;

  const string TFS_url = "<TFS_URL>";
  string projectname = "<Teamname>"; 
  TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(TFS_url));
  // Get build information
  BuildHttpClient bhc = ttpc.GetClient<BuildHttpClient>();

  // Get testrun for the build
  TestManagementHttpClient ithc = ttpc.GetClient<TestManagementHttpClient>();

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

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

  List<List<TestCaseResult>> allTestresults = new List<List<TestCaseResult>>();

  foreach (TestRun testrun in testruns)
  {
    List<TestCaseResult> testresults = ithc.GetTestResultsAsync(projectname, testrun.Id).Result;
    allTestresults.Add(testresults);
  }

I would prefer to use the given classes in C# and not having to do it via the API.

Upvotes: 1

Views: 1087

Answers (1)

PatrickLu-MSFT
PatrickLu-MSFT

Reputation: 51073

The test result of the build/release is stored in test runs, so you just need to get the test run of the build/release first and then retrieve the test result from the test run.

You can get test run id from the test log for a specific release.

So, the simplest way is getting the specific test run id with the REST API from the log.

GET https://{instance}/{project}/_apis/release/releases/{releaseId}/environments/{environmentId}/deployPhases/{releaseDeployPhaseId}/tasks/{taskId}/logs?api-version={version}

See Get Task Log v5 for details.

Then just through test run ID to get test results details. Similar with your shared code sample for build.

Upvotes: 1

Related Questions