首先利用@DataProvider(name = "XXX")的属性,将name的值XXX 传递给 @Test(dataProvider = "XXX")
看以下一段代码:
package com.course.testng.parameter; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.lang.reflect.Method; public class DataProviderTest { @Test(dataProvider = "data") public void testDataProvider(String name,int age){ System.out.println("name = " + name + ",age = " + age); } @DataProvider(name = "data") public Object[][] providerData(){ Object[][] o = new Object[][]{ {"zhangsan",10}, {"lisi",20}, {"wangwu",30} }; return o; } @Test(dataProvider = "methodData") public void test1(String name,int age){ System.out.println("test1: name = " + name + ",age = " + age); } @Test(dataProvider = "methodData") public void test2(String name,int age){ System.out.println("test2: name = " + name + ",age = " + age); } @DataProvider(name = "methodData") public Object[][] methodDataTest(Method method){ Object[][] result = null;
//method.getName().equals("方法名字,和方法名字匹配传值") if (method.getName().equals("test1")){ result = new Object[][]{ {"zhangsan",20}, {"lisi",25}}; }else if(method.getName().equals("test2")){ result = new Object[][]{ {"wangwu",50}, {"zhaoliu",60} }; } return result; } }
结果:
test1: name = zhangsan,age = 20
test1: name = lisi,age = 25
test2: name = wangwu,age = 50
test2: name = zhaoliu,age = 60
name = zhangsan,age = 10
name = lisi,age = 20
name = wangwu,age = 30
===============================================
Default Suite
Total tests run: 7, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0