Have you ever wanted to automate the process of updating the "build in use" field within your Test Manager Test Plan, and just couldn't believe that this feaure could be missing from TCM.exe?

Well, I had the same issue a few days back and decided to utilize the TFS API and write a console application that did this for me. Now I can launch a new Test Run with TCM.exe after running this tool, and the test run will be executed on the selected build and results will be published and reported on the selected build.....

...keep in mind that this needs to be executed on a machine with VS2010 installed:

Usage: TestPlanBuildUpdate.exe <BuildDefinition> <BuildNumber> <TestPlanID>

 

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;
using Microsoft.TeamFoundation.Build.Client;

namespace TestPlanBuildUpdate
{
    class Program
    {
        static int Main(string[] args)
        {
            //Define the variables
            string serverurl = "http://<yourtfsserver>:8080/tfs";
            Uri configurationServerUri = new Uri(serverurl);
            string project = "<yourprojectname>";
            string branch = "";
            int myPlansId = 0;
            string buildNumber = "";
            if (args.Length != 3)
            {
                Console.WriteLine("\n Usage: TestPlanBuildUpdate.exe branch buildnumber testplanid");
                Console.WriteLine("\n Press any key to exit...");
                Console.ReadLine();
                return 1;
            }
            else
            {
                try
                {
                    branch = args[0];
                    buildNumber = args[1];
                    myPlansId = int.Parse(args[2]);
                }
                catch (Exception)
                {
                    Console.WriteLine("\n Error parsing arguments, arguments should be in format <string> <string> <int> \n");
                    return 1;
                }
            }

            try
            {
                //Get the build uri needed from build number
                TfsTeamProjectCollection tfsserv = new TfsTeamProjectCollection(configurationServerUri);
                IBuildServer buildServer = (IBuildServer)tfsserv.GetService(typeof(IBuildServer));
                IBuildDetailSpec buildDetailSpec = buildServer.CreateBuildDetailSpec(project, branch);
                buildDetailSpec.BuildNumber = buildNumber;
                IBuildQueryResult results = buildServer.QueryBuilds(buildDetailSpec);
                IBuildDetail buildDetail = results.Builds[0];
                Console.WriteLine("Build: {0}", buildDetail.BuildNumber);
                Console.WriteLine("Build URI: {0}", buildDetail.Uri);

                //Update the test plan
                ITestManagementTeamProject proj = GetProject(serverurl, project);
                ITestPlan plan = proj.TestPlans.Find(myPlansId);
                Console.WriteLine("Test Plan: {0}", plan.Name);
                Console.WriteLine("Plan ID: {0}", plan.Id);
                Console.WriteLine("Previous Build Uri: {0}", plan.PreviousBuildUri);
                Console.WriteLine("Build Currently In Use: {0}", plan.BuildNumber);
                plan.BuildUri = buildDetail.Uri;
                Console.WriteLine("\n Build updated in Test Plan \n");
                Console.WriteLine("Build Currently In Use: {0}", plan.BuildNumber);
                plan.Save();
                return 0;
            }
            catch (Exception e)
            {
                Console.WriteLine("\n There was an error \n");
                Console.WriteLine(e);
                return 1;
            }
        }

        static ITestManagementTeamProject GetProject(string serverUrl,
            string project)
        {
            TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName(serverUrl));
            ITestManagementService tms = tfs.GetService<ITestManagementService>();

            return tms.GetTeamProject(project);
        }

    }
}