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 = "http://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);
        }
    }
}

Comments (4)

  1. Drew says:

    When I use "GetProject", it doesn't resolve to an object.

    Suggestions?

  2. Natarajan says:

    Can I redistribute the

    Microsoft.TeamFoundation.Client.dll

    Microsoft.TeamFoundation.TestManagement.Client.dll

    Microsoft.TeamFoundation.TestManagement.Common.dll

    Microsoft.TeamFoundation.WorkItemTracking.Client.dll

    with My custom application/product or would I need to have Visual studio installed wherever I need to use this integration

  3. jd says:

    Oddly, I ran into a situation where two machines both had "microsoft.teamfoundation.testmanagement.client.dll" on them with the same version and public key.  They differed in the content they contained though.  The newer one had additional classes.  Why would the content of the dll change while the version and public key stay the same?  Very, very time consuming and frustrating to debug the errors that ensued.