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



我正在尝试获取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
}

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

单元测试适配器无法连接到数据源或读取数据。有关故障排除此错误的更多信息,请参阅"数据驱动单元测试故障排除"(http://go.microsoft.com/fwlink/?LinkId=62412)MSDN Library中。错误详细信息:找不到请求的.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];

也会起作用。

最新更新