Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / TFS

Retrieving TFS Test Case Parameters using C#

5.00/5 (1 vote)
18 May 2019CPOL1 min read 6.8K  
How to retrieve TFS test case parameters in C#

Introduction

This is a quick tutorial on how to retrieve TFS test case parameter values using C#.

Background

If your project is connected to TFS and for some reason, you need to access the values of parameters defined in one or more testcases, you need to add Microsoft.TeamFoundationServer.ExtendedClient NuGet package to your solution. It provides the functionality you need to retrieve info from TFS.

Using the Code

The code below is a console application that connects to the TFS server, finds the "My Project" project and prints out the values of the parameters for the test case with id = 3 (see below).

Image 1

C#
using System;
using System.Data;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.TestManagement.Client;
 
namespace TfsParameters
{
    class Program
    {
        static void Main(string[] args)
        {
            var parameterValues = TfsManager.Instance.GetParamsValues(3);
            foreach(var row in parameterValues)
            {
                foreach (string value in row)
                {
                    Console.Write(value + "\t");
                }
                Console.WriteLine();
            }                
        }        
    }
 
    public class TfsManager
    {
        const string tfsUrl = "https://TfsServerAddress/";
        const string projectName = "My Project";
        private static ITestManagementTeamProject Project;
 
        private static Lazy<TfsManager> _instance = 
                       new Lazy<TfsManager>(() => new TfsManager());
        public static TfsManager Instance => _instance.Value; 
 
        private TfsManager()
        {
            TfsTeamProjectCollection tfsCollection = 
            TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsUrl));
            tfsCollection.Connect(ConnectOptions.IncludeServices);
 
            ITestManagementService tmService = 
                 tfsCollection.GetService<ITestManagementService>();
            Project = tmService.GetTeamProject(projectName);
        } 
 
        public List<List<string>> GetParamsValues(int testCaseId)
        {
            List<List<string>> result = new List<List<string>>();
 
            ITestCase test = Project.TestCases.Find(testCaseId);
            DataTable parametersTable = test.DefaultTableReadOnly;
 
            foreach (DataRow dr in parametersTable.Rows)
            {
                var row = new List<string>();
                for (var i = 0; i < parametersTable.Columns.Count; i++)
                {
                    row.Add((string)dr[i]);
                }
                result.Add(row);
            }
 
            return result;
        }
    }
}

The TfsManager constructor takes care of the connection to the TFS server and initializes the Project object. The actual parameter values are retrieved by the GetParamsValues method, which first gets a reference to the test case with the specified ID and then gets the DefaultTableReadOnly data table.

The strings in the table are saved in result and returned. Printing the result to console will output:

1234 how
2007 are
881 you

You can also retrieve the names of the parameters (MyNumberParam and MyStringParam) by using the ColumnName property:

C#
parametersTable.Columns[i].ColumnName

or check if there even are any parameters defined in the testcase by checking the number of elements in the TestParameters collection:

C#
TestParameterCollection params = test.TestParameters;

Of course, there is plenty more functionality that you might find useful provided by the ITestManagementService and ITestCase interfaces, but that is beyond the scope of this article.

History

  • 18th May, 2019: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)