繁体   English   中英

如何在 C# 测试中从 azure devops(以前的 vsts)的测试用例中获取参数值?

[英]How to get parameter values from a test case on azure devops (former vsts) in C# tests?

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

我正在尝试在看起来像这样的测试方法中获取值-

[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

}

在运行这个测试时,它甚至没有进入测试方法代码。 它给出了这个错误 -

单元测试适配器无法连接到数据源或读取数据。 有关解决此错误的详细信息,请参阅 MSDN 库中的“数据驱动单元测试疑难解答”( http://go.microsoft.com/fwlink/?LinkId=62412 )。 错误详细信息:无法找到请求的 .Net Framework 数据提供程序。 它可能没有安装。

测试方法错误

拜托,任何人都可以指出我在这里缺少的东西吗? 我遵循了数据驱动单元测试文档。 但我觉得我可能会遗漏一些可以让它发挥作用的东西。 谢谢!

我正在回答我自己的问题。 我通过使用 Microsoft.TeamFoundation.WorkItemTracking.WebApi、Microsoft.VisualStudio.Services.Common 和 Microsoft.VisualStudio.Services.WebApi 命名空间让它工作。

代码是这样的

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

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
    }
  }

我希望它能帮助别人。 从此链接获得帮助以进行身份验证

https://learn.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/pats?view=vsts

感谢您添加解决方案。 我试过了,但从 GAC Fusion Prober 得到了一些装配错误。 但是我找到了一种更简单的方法来做到这一点,我想分享一下。

请参阅: https://blogs.infosupport.com/accessing-test-case-parameters-in-an-associated-automation/

所有你需要的是:

private void PrintParameterValues(ITestCase testCase, string parameterName)
{
    foreach(DataRow row in testCase.DefaultTableReadOnly.Rows)
    {
        string value = row[parameterName];
        Console.WriteLine(parameterName + " value: " + value;
    }
}

它适用于我的案例,我能够从我的参数中打印出所有值。

您还可以使用索引而不仅仅是参数名称:

string value = row[0];

也会工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM