testng dataprovider 的几种用法以及Java中的二维数组

时间:2023-01-31 17:51:39

@Log4j
public class testDataprovider { @DataProvider(name = "DP1") public Object [][] createData() { Object[][] retObjArr = { {"001","Jack","London"}, {"002","John","New York"}, {"003","Mary","Miami"}, {"004","George","California"} }; return retObjArr; } @Test(dataProvider = "DP1") public void testEmployeeData(String empid, String empName, String city) { log.info(empid); log.info(empName); log.info(city); }
二、直接使用二维数组
@DataProvider(name = "datasource")
    public Object[][] createDataSource(){

        return new Object[][]{
                {1,2,3,3},
                {2,1,3,3},
                {5,3,4,5},
                {2,3,2,3}
        };
    }

    @Test(dataProvider = "datasource")
    public void verifyGetMaxResult(int fir, int sec, int thd,int result)
    {
         assert result == calc.getMax(fir,sec,thd);
    }

三、使用Map<string, string>

    @SneakyThrows
    @DataProvider(name = "refund")
    private Map[][] testDataForRefund() {
        String excelFile = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "data" + File.separator + "webgate" + File.separator + "TestData.xlsx";
        ExcelUtil.read(excelFile);
        return ExcelUtil.getData("退款");
    }

@Test(dataProvider = "refund")
    public void TestRefund01(Map<String, String> data) throws Exception {
        executeTestCase(data);
    }

 上面可以看出dataprovider生成的是Java的二维数组,下面讲一下Java的二维数组:

//定义二维数组写法2        不规则数组
class numfive
{
public static void main(String[] args)
{
long[][] numfive=new long[5][];     //定义一个long类型的不规则数组
numfive[0]=new long[5];             //为第1行分配5列
numfive[1]=new long[6];             //为第2行分配6列
numfive[2]=new long[7];             //为第3行分配7列
numfive[3]=new long[8];             //为第4行分配8列
numfive[4]=new long[9];             //为第5行分配9列
numfive[0][4]=10000000000L;         //1行5列=10000000000
numfive[1][5]=20000000000L;         //2行6列=20000000000
numfive[2][6]=30000000000L;         //3行7列=30000000000
numfive[3][7]=40000000000L;         //4行8列=40000000000
numfive[4][8]=50000000000L;         //5行9列=50000000000
System.out.println(numfive[0][4]); //打印换行输出喽
System.out.println(numfive[1][5]);
System.out.println(numfive[2][6]);
System.out.println(numfive[3][7]);
System.out.println(numfive[4][8]);
System.out.println(numfive[4][7]); //打印输出一个没有定义数组元素的数组 java会自动将他初始化值为0
}
}
//定义二维数组写法3   定义的同时分配空间大小
class numfour
{
public static void main(String[] args)
{
   short[][] numfour=new short[5][8]; //定义一个short类型的数组同时为它分配5行8列的空间大小
   numfour[0][7]=10;
   numfour[1][6]=20;
   numfour[2][5]=30;
   numfour[3][4]=40;
   numfour[4][3]=50;
   System.out.println(numfour[0][7]);
   System.out.println(numfour[1][6]);
   System.out.println(numfour[2][5]);
   System.out.println(numfour[3][4]);
   System.out.println(numfour[4][3]);
}
}