2
votes

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.

Test method error

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!

1
Possible duplicate from here: stackoverflow.com/questions/46105558/… Have you checked with this one?Rob Bos
I have. It did not work I am afraid. :(Safkat

1 Answers

2
votes

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://docs.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://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/pats?view=vsts