3

Scenario: I have a csv file with 10 columns of test data. For each column I want to have a test method.

Now I know how to use dataprovider to read the csv file and provide the test data to a test method. But how can I use the same testprovider for multiple tests?

The dataprovider that I have written for now is reading the csv file and iterating through the csv.

4

2 回答 2

2

如果我正确理解您的问题,那么您想要做的是假设您有 10 列,这 10 列需要分别作为测试数据传递给 10 个测试方法,但您希望数据提供者相同。我的建议:1)将 Method 参数传递给您的数据提供者。2) 将整个 CSV 文件加载到二维数组中。3) 基于返回该列数据作为该测试的测试数据的测试方法名称。如下所示:

import java.lang.reflect.Method;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestNGTest {
    @DataProvider
    public Object[][] dp(Method method)
    {
        System.out.println("Test method : "+method.getName());
        if(method.getName().equals("test1"))
            return new Object[][]{{method.getName()}};
        else if(method.getName().equals("test2"))
            return new Object[][]{{method.getName()}};
        else
            return new Object[][]{};
    }

    @Test(dataProvider="dp")
    public void test1(String name)
    {
        System.out.println("DP -->"+name);
    }

    @Test(dataProvider="dp")
    public void test2(String name)
    {
        System.out.println("DP -->"+name);
    }
}
于 2014-09-30T04:18:50.633 回答
1

您可以轻松地在单独的类中声明数据提供者并在多个类中重用它。看一下@Test 注解的 dataProviderClass 参数

于 2014-09-30T08:26:15.350 回答