TFS API: How to get latest test result for a test case

We can retrieve test results for particular test case using TFS API. That's straight forward and there are in build methods. I find people asking for how they can retrieve latest test result for particular test case and I got it solved so thought of sharing here. below code snippet will illustrate how to -

1. Connect to TFS Server/Collection/team project

Uri tfsUri = new Uri("http://servername:8080/tfs/collectionname");
teamProjectCollection = new TfsTeamProjectCollection(tfsUri);
 
iTestManagementService = teamProjectCollection.GetService<ITestManagementService>();
tfsConnectedTeamProject = iTestManagementService.GetTeamProject("Team Project Name");
 
2. Once connected to TFS server, call APIs to get test results for any particular test case. Below line of code will get all results associated with passed test case Id and this returned list is not sorted

var testResults = tfsConnectedTeamProject.TestResults.ByTestId(52737);

3. We can sort this using below code using LastUpdated property of ITestCaseResult and it will return you sorted list of test result.

var resSort = from res in testResults
orderby res.LastUpdated descending
select res;
 
4. Now if you need only latest test result from result collection then below is code. Use above sorted list and retrieve top most record.

var latestTestResult = resSort.First<ITestCaseResult>();

Hope this help you all.

Thanks,
Vivek

Comments

Popular posts from this blog

How To: Bulk upload Test Case results to Microsoft Test Manager

TFS API: Get Work Item History Revisions for parituclar Field

Add attachment to Test Case Result using TFS API