gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:30:17 25 4
gpt4 key购买 nike

我正在尝试获取 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 库中的“数据驱动单元测试故障排除”( 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://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

关于azure - 如何在 C# 测试中从 azure devops(以前的 vsts)上的测试用例获取参数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53560461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com