Reputation: 61
I am trying to get the parameter values defined in a test case in Azure DevOps (former VSTS). My test case looks like this- Azure devops test case
I am trying to get the values in a test method that looks like this-
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.TestCase",
"https://[companyName].visualstudio.com;[projectName]",
"5843", // this is the test case number
DataAccessMethod.Sequential),
TestMethod]
public void DataOverlapsBottomRowOfFilterFromTestParameter()
{
string column1 = TestContext.DataRow[0].ToString(); // read parameter by column index
string column2 = TestContext.DataRow["Column2"].ToString(); //read parameter by column name
// rest of the code
}
While running this test it does not even come into the test method code. It gives this error-
The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see "Troubleshooting Data-Driven Unit Tests" (http://go.microsoft.com/fwlink/?LinkId=62412) in the MSDN Library. Error details: Unable to find the requested .Net Framework Data Provider. It may not be installed.
Please, can anyone point me out what I am missing here? I have followed the Data-Driven Unit Test documentation. But I feel I might be missing something that can make it work. Thanks!
Upvotes: 2
Views: 3365
Reputation: 1
Thanks For Adding your Solution. I tried it but got Some Assembly Errors from GAC Fusion Prober. But I Found an Easier Way to do that and I would like to share.
see :https://blogs.infosupport.com/accessing-test-case-parameters-in-an-associated-automation/
All you need is :
private void PrintParameterValues(ITestCase testCase, string parameterName)
{
foreach(DataRow row in testCase.DefaultTableReadOnly.Rows)
{
string value = row[parameterName];
Console.WriteLine(parameterName + " value: " + value;
}
}
It Worked in My Case and I Was Able to Print out all Values from my Parameter.
You Can also Use index and not only Parameter name :
string value = row[0];
will also work.
Upvotes: 0
Reputation: 61
I am answering my own question. I got it working by using the Microsoft.TeamFoundation.WorkItemTracking.WebApi, Microsoft.VisualStudio.Services.Common and Microsoft.VisualStudio.Services.WebApi namespaces.
The code goes like this
[TestMethod]
[WorkItem(1111)]
public void GetTestValuesFromTestParameter()
{
//This test is for continuous range
var method = MethodBase.GetCurrentMethod();
var attr = (WorkItemAttribute)method.GetCustomAttributes(typeof(WorkItemAttribute), true)[0];
var workItemId = attr.Id;
var dataTable = GetTableItemsFromTestCase(workItemId);
foreach (DataRow dataRow in dataTable.Rows)
{
//Rest of the code
}
}
GetTableItemsFromTestCase method -
private DataTable GetTableItemsFromTestCase(int workItemId)
{
var accountUri = new Uri(""); // Account URL, for example: https://fabrikam.visualstudio.com
var personalAccessToken = "; // See https://learn.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/pats?view=vsts
// Create a connection to the account
var connection = new VssConnection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken));
// Get an instance of the work item tracking client
var witClient = connection.GetClient<WorkItemTrackingHttpClient>();
IEnumerable<XElement> descendants = new List<XElement>();
var dt = new DataTable();
try
{
// Get the specified work item
var workitem = witClient.GetWorkItemAsync(workItemId).Result;
var itemParams = workitem.Fields["Microsoft.VSTS.TCM.Parameters"];
var itemParamsElement = XElement.Parse((string)itemParams);
var paramDataSource = workitem.Fields["Microsoft.VSTS.TCM.LocalDataSource"];
var xElement = XElement.Parse(paramDataSource.ToString());
//Assuming we have a table named "Table1" in the workitem
descendants = xElement.Descendants("Table1");
foreach (var xe in itemParamsElement.Descendants("param"))
{
var name = xe.Attribute("name").Value;
dt.Columns.Add(name, typeof(string));
}
foreach (var descendant in descendants)
{
var r = dt.NewRow();
foreach (var xe in descendant.Descendants())
{
r[xe.Name.LocalName] = xe.Value;
}
dt.Rows.Add(r);
}
}
catch (AggregateException aex)
{
VssServiceException vssex = aex.InnerException as VssServiceException;
if (vssex != null)
{
//log error
}
}
I hope it helps others. Got help from this link for authentication
https://learn.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/pats?view=vsts
Upvotes: 3