The Test Management API – Getting Started (Updated for RC Release)

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 Manager (MTM). 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)
        {
            TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(TfsTeamProjectCollection.GetFullQualifiedUriForName(serverUrl));
            ITestManagementService tms = tfs.GetService<ITestManagementService>();

            return tms.GetTeamProject(project);
        }
    }
}