我如何使用mstest或nunit将DataTable用作测试用例的数据源?

问题描述 投票:-1回答:1

我有一个包含数据的数据表,并且我想将数据表用作测试用例的参数,作为数据驱动的测试。

任何测试框架(MsTest / Nunit)建议都可以使用c#作为脚本语言。

场景:-

我需要从TFS获取测试数据,我可以将其检索并将其存储在数据表中。保存到数据表后,我需要在测试用例中使用相同的数据表作为参数,以便对所有参数运行测试用例。

    [DataTestMethod]
    [WorkItem(13)]
    public void GetTestValuesFromTestParameter()
    {
        //Code to get the data from TFS
       var method = MethodBase.GetCurrentMethod();
       var attr = (WorkItemAttribute)method.GetCustomAttributes(typeof(WorkItemAttribute), true)[0];
       GetTableItemsFromTestCase(workItemId);
    }

    private DataTable GetTableItemsFromTestCase(int workItemId)
    {
        //Return the data table items from TFS
    }

我很震惊,需要有关如何将数据表传递到测试用例的帮助。

c# nunit mstest data-driven-tests data-driven
1个回答
0
投票

数据测试方法将允许使用DataRow属性将数据传递到测试。从那里,您应该能够调用数据表代码并执行所需的测试

[DataTestMethod]
[DataRow(13)]
[DataRow(18)]
//...
[DataRow(nn)]
public void GetTestValuesFromTestParameter(int workItemId) {
    //Code to get the data from TFS
    DataTable data = GetTableItemsFromTestCase(workItemId);

    //...
}

private DataTable GetTableItemsFromTestCase(int workItemId) {
    //Return the data table items from TFS
}
© www.soinside.com 2019 - 2024. All rights reserved.