testNG传参数的两种方式(xml文件,@DataProvider)
使用testng.xml设置参数
参数在xml文件中可以在suite级别定义,也可以在test级别定义;testNG会尝试先在包含当前类的test标签中寻找参数,如果没找到则在上层的suite标签中查找。即在test标签中相同的参数对当前类当前方法的优先级比较高。 testNG支持这种传参方式的类型如下:String、 int/Integer、boolean/Boolean、 byte/Byte、 char/Character、
double/Double、 float/Float、 long/Long、 short/Short。对于非上述类型TestNG无法通过这种方式进行传参,可以通过@DataProvider方式传参 public class ParameterTest { /** * Following method takes one parameter as input. Value of the * said parameter is defined at suite level. */ @Parameters({ "suite-param" }) @Test public void prameterTestOne(String param) { System.out.println("Test one suite param is: " + param); } /** * Following method takes one parameter as input. Value of the * said parameter is defined at test level. */ @Parameters({ "test-two-param" }) @Test public void prameterTestTwo(String param) { System.out.println("Test two param is: " + param); } /** * Following method takes two parameters as input. Value of the * test parameter is defined at test level. The suite level * parameter is overridden at the test level. */ @Parameters({ "suite-param", "test-three-param" }) @Test public void prameterTestThree(String param, String paramTwo) { System.out.println("Test three suite param is: " + param); System.out.println("Test three param is: " + paramTwo); } } |