Partager via


Query Test Run Results by Build

Here is the piece of code to query test run results from MTM 2010 for given build:

Step 1: Get the Build Uri for given Build Name

 

public Uri GetBuildUri(string BuildName)

{

    Uri buildUri = null;

    if (string.IsNullOrEmpty(BuildName))

        return null;

IBuildServer buildServer = (IBuildServer)(((TfsTeamProjectCollection) <<TeamProjectCollection>> ).GetService(typeof(IBuildServer)));

// create spec instance and apply the filter like build name, date time

   IBuildDetailSpec spec = buildServer.CreateBuildDetailSpec(GetProject().Name);

    spec.BuildNumber = <<BuildName>>; //Example - "Daily_20110502.4";

    IBuildQueryResult buildDetails = buildServer.QueryBuilds(spec);

    if (buildDetails != null)

        buildUri = (buildDetails.Builds[0]).Uri;

    return buildUri;

}

Step 2: Get the TestRun for the given BuildUri

 

public IEnumerable<ITestRun> GetTestResultsByBuild(ITestManagementTeamProject testManagementTeamProject, Uri BuildUri)

{

if (testManagementTeamProject == null)

    return null;

    IEnumerable<ITestRun> testRuns = testManagementTeamProject.TestRuns.ByBuild(BuildUri);

    return testRuns;

}

Step 3: Loop through the TestRuns to display the Test Case output.

  

foreach (ITestRun testRun in testRuns)

{

    foreach (ITestCaseResult result in testRun.QueryResults())

    {

System.Console.WriteLine(string.Format("TestCaseID:{0}",result.TestCaseId.ToString()));

System.Console.WriteLine(string.Format("TestCaseID:{0}",result. Priority.ToString()));

System.Console.WriteLine(string.Format("TestCaseID:{0}",result. TestCaseTitle.ToString()));

System.Console.WriteLine(string.Format("TestCaseID:{0}",result. Outcome.ToString()));

System.Console.WriteLine(string.Format("TestCaseID:{0}",result. DateStarted.ToString()));

System.Console.WriteLine(string.Format("TestCaseID:{0}",result. DateCompleted.ToString()));

    }

}