如何在C#测试中从Azureure Devops(以前的vsts)的测试用例中获取参数值? [英] How to get parameter values from a test case on azure devops (former vsts) in C# tests?

查看:139
本文介绍了如何在C#测试中从Azureure Devops(以前的vsts)的测试用例中获取参数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取在Azure DevOps(以前的VSTS)的测试用例中定义的参数值.我的测试用例看起来像这样- Azure devops测试用例

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-

单元测试适配器无法连接到数据源或读取数据.有关对此错误进行故障排除的详细信息,请参阅对数据驱动的单元测试进行故障排除"( http://go.microsoft.com/fwlink/?LinkId=62412 ).错误详细信息:无法找到请求的.Net Framework数据提供程序.可能未安装.

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!

推荐答案

我正在回答自己的问题.我通过使用Microsoft.TeamFoundation.WorkItemTracking.WebApi,Microsoft.VisualStudio.Services.Common和 Microsoft.VisualStudio.Services.WebApi命名空间.

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.

代码像这样

[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方法-

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/zh-CN/azure/devops/integrate/get-started/authentication/pats?view=vsts

这篇关于如何在C#测试中从Azureure Devops(以前的vsts)的测试用例中获取参数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆