This article is a great resource, actually it was the only one I had found available when I was searching something similar.
In general you need access to ITestManagementService
.
Given you already have connection to a teamProjectCollection
and a buildDetail
, something like this should work out for you :
var tstService = (ITestManagementService)teamProjectCollection.GetService(typeof(ITestManagementService));
ITestManagementTeamProject testManagementTeamProject = tstService.GetTeamProject(buildDetail.TeamProject);
IEnumerable<ITestRun> testRuns = testManagementTeamProject.TestRuns.ByBuild(buildDetail.Uri);
foreach (var testRun in testRuns)
{
ITestCaseResultCollection testcases = testRun.QueryResultsByOutcome(TestOutcome.Failed);
foreach (var testcase in testcases)
{
Console.WriteLine("TestCase ID: " + testcase.TestCaseId);
Console.WriteLine("TestCase Title: " + testcase.TestCaseTitle);
Console.WriteLine("Error Message: " + testcase.ErrorMessage);
}
}
(This code is basically a copy from the article above, it is work by Anuj Chaudhary)
Remember to add "Microsoft.TeamFoundation.TestManagement.Client" in your ref list.