标签:div provider XML class custom sha new param 需要
testNG @DataProvider参数化是用java 代码写个用@DataProvider标记专门为测试方法提供参数的方法。该方法上用@DataProvider(name = "data")标记,需要被执行的测试用例方法需要用@Test()标识且在@Test中添加dataProvider 参数,如@Test(dataProvider = "data") 。
注意事项:@DataProvider中的name参数的值 必须和@Test中的dataProvider 参数的值一致。如果不一致 则无法调用到@DataProvider(name = "data")参数化方法中的参数。
@DataProvider参数化不止可以直接用于单个测试用例上,也可以用过方法名运用于多个测试用例上。
2 3 import org.testng.annotations.DataProvider; 4 import org.testng.annotations.Test; 5 6 import java.lang.reflect.Method; 7 8 public class DataProviderTest { 9 /** 10 * 直接运行在测试用例上的参数化 11 * 12 * @param name 13 * @param age 14 */ 15 @Test(dataProvider = "data") 16 public void testDataProvider(String name, int age) { 17 System.out.println("name=" + name + " age=" + age); 18 } 19 20 @DataProvider(name = "data") 21 public Object[][] ProviderData() { 22 Object[][] o = new Object[][]{ 23 {"zhangshan", 10}, 24 {"lisi", 20} 25 26 }; 27 return o; 28 } 29 30 31 @Test(dataProvider = "methodData") 32 public void test1(String name, int age) { 33 System.out.println("test11111方法" + "name=" + name + " age=" + age); 34 35 } 36 37 @Test(dataProvider = "methodData") 38 public void test2(String name, int age) { 39 System.out.println("test22222方法" + "name=" + name + " age=" + age); 40 41 } 42 43 /** 44 * DataProvider 运用在方法上,根据方法名进行参数化 45 * 46 * @param method 47 * @return 48 */ 49 @DataProvider(name = "methodData") 50 public Object[][] methodDataTest(Method method) { 51 Object[][] result = null; 52 if (method.getName().equals("test1")) { 53 result = new Object[][]{ 54 {"zhangshan", 10}, 55 {"lisi", 20} 56 }; 57 } else if (method.getName().equals("test2")) { 58 result = new Object[][]{ 59 {"wangwu", 10}, 60 {"molin", 20} 61 }; 62 } 63 return result; 64 } 65 }
C:\Users\Administrator\.IntelliJIdea2019.3\system\temp-testng-customsuite.xml test11111方法name=zhangshan age=10 test11111方法name=lisi age=20 test22222方法name=wangwu age=10 test22222方法name=molin age=20 name=zhangshan age=10 name=lisi age=20 =============================================== Default Suite Total tests run: 6, Failures: 0, Skips: 0 =============================================== Process finished with exit code 0
标签:div provider XML class custom sha new param 需要
原文地址:https://www.cnblogs.com/linxinmeng/p/12593512.html