Saturday, September 29, 2012

Querying TFS Test Managment Service from C#

Query Test Plans from a C# app


Our shop uses Visual Studio 2010 and Microsoft Test Manager (MTM) for entering and running manual test cases. They wondered how to export a flat list of test cases to Excel that captured all the test case steps and the result of the last test pass. Interestingly, we couldn't find any way to do it from MTM or Visual Studio.

I did some searching and cobbled together a little Winform app in C# that has worked great for our purposes. I removed error handling for brevity but left in the core stuff.

Set References to the TFS APIs

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

First you can get a List of all your Team Projects containing Test Plans

(You need to know the URI of your TFS server)
public List<string> GetTeamProjects()
{
   var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(TestPlanQueryTool.Properties.Settings.Default.TfsUri));
      var service = tfs.GetService<VersionControlServer>();
      return service.GetAllTeamProjects(true).Select(i => i.Name).ToList();
}

I used the List of projects to populate a DropDown ComboBox.
On selecting a team project, I would....

Get a List of Test Plans for the Selected Team Project

public List<string> GetTestPlans(string teamProject)
{
    var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
            new Uri(TestPlanQueryTool.Properties.Settings.Default.TfsUri));
    var service = tfs.GetService<ITestManagementService>();
    ITestManagementTeamProject teamProject = service.GetTeamProject(teamProject);
    return _teamProject.TestPlans.Query("SELECT * FROM TestPlan").Select (i => i.Name).ToList();
}

I populated a listview control with the list of Test Plans. Now I needed to query all the test suites contained in the test plan.



No comments:

Post a Comment