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).
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 string
s 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:
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:
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