如何在 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 test case

我正在尝试在如下所示的测试方法中获取值-

[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 库中的 "Troubleshooting Data-Driven Unit Tests" (http://go.microsoft.com/fwlink/?LinkId=62412)。错误详细信息:无法找到请求的 .Net Framework 数据提供程序。可能没有安装。

Test method error

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

我正在回答我自己的问题。我通过使用 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://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
    }
  }

希望对大家有所帮助。从这个 link 获得了身份验证的帮助

https://docs.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];

也可以。