The Test Management API - Getting Started
Overview
This post kicks off a series that will explore the Test Management API’s. Using these API’s you can accomplish many tasks that may not be possible via the UI (Microsoft Test and Lab Manager (MTLM) code named Camano). Perhaps you have test assets in another system for example, with the API you could create your own solution that fits your individual needs for migrating data between systems. Or maybe you have a custom tool in mind for your team that will need to be able to examine the various objects you’ve stored in the test case management component of TFS, such as your Test Plans, Test Cases, Test Runs, Results, etc.
Whatever your reason, using the Test Management API’s will almost always have a common starting point, you first need to establish a connection to the Team Foundation Server and that’s what I’ll cover here.
The Code
You’ll need to add references to the following assemblies to your project:
Microsoft.TeamFoundation.Client.dll
Microsoft.TeamFoundation.TestManagement.Client.dll
Microsoft.TeamFoundation.TestManagement.Common.dll
Microsoft.TeamFoundation.WorkItemTracking.Client.dll
These will all be GAC’d on a machine you’ve installed VSTS on.
Here’s a quick and easy sample C# console application that connects to the server and creates a very basic test case.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
namespace BlogSamples
{
class Program
{
static void Main(string[] args)
{
string serverurl = "https://localhost:8080/tfs";
string project = "Beta1";
ITestManagementTeamProject proj = GetProject(serverurl, project);
ITestCase tc = proj.TestCases.Create();
tc.Title = "Test";
tc.Save();
Console.WriteLine("TC: {0}", tc.Id);
}
static ITestManagementTeamProject GetProject(string serverUrl,
string project)
{
TeamFoundationServer tfs = new TeamFoundationServer(serverUrl);
ITestManagementService tms = tfs.GetService<ITestManagementService>();
return tms.GetTeamProject(project);
}
}
}
Next Time
In my next post I’ll start going over the core objects in the Test Management object model in more detail.
Comments
Anonymous
November 20, 2009
This thread really got me started on my quest to migrate from Test Director to TFS 2010 :) Thanks! I look forward to your next post!!Anonymous
December 16, 2010
Hi Dennis, Can you pls help me by provide API ref for uploading Test Results to TFS 2010?, I am excecuting test cases outside TFS, I want to upload the results into TFS. Thanks kkcAnonymous
February 01, 2012
Hi Dennis, While using the GetProject Function I am getting the following exception "Microsoft.TeamFoundation.TestManagement.Client.TestObjectNotFoundException". Can you please help me in solving this?