TestNG可以通过testng.xml和Data Providers向测试方法传递参数
利用testNG.xml传递参数
1-创建一个TestNG测试类
其中 parameters = {"xml-file","hostname"} 使用来接受参数的,因为有两个值所以有两个参数
package com.lc.testChuanCan; import org.testng.annotations.Test; public class testNG05 { @Test(parameters = {"xml-file","hostname"}) public void testNG05_01(String xmlfile,String hostname) { System.out.println("我是testNG05 类的testNG05——01方法, 我传递参数是 xml-file:"+xmlfile+" hostname:"+hostname); } }
2-创建一个TestNG.xml文件
设置参数标签
<parameter name="xml-file" value="accounts.xml"></parameter> <parameter name="hostname" value="arkonis.example.com"></parameter>
标签:parameter
anme:相当于key ,变量名称;对应TestNG测试类的 parameters = {"xml-file","hostname"} 里面参数名称,名字要一样
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite" parallel="none"> <parameter name="xml-file" value="accounts.xml"></parameter> <parameter name="hostname" value="arkonis.example.com"></parameter> <test name="Test"> <classes> <class name="com.lc.testChuanCan.testNG05"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
parameter也可在<test></test>里面添加参数
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<!--在测试suit里添加参数;在这里面添加的参数测试类都可以引用;但在test添加的参数上一级不可以使用--> <suite name="Suite" parallel="none"> <parameter name="xml-file" value="accounts.xml"></parameter> <parameter name="hostname" value="arkonis.example.com"></parameter> <test name="Test"> <classes> <class name="com.lc.testChuanCan.testNG05"/> </classes> </test> <!-- Test -->
<!-- 在test里添加参数 testKey--> <test name="test01"> <parameter name="testKey" value="我是test标签里面的参数"></parameter> <classes> <class name="com.lc.testChuanCan.testNG6"></class> </classes> </test> </suite> <!-- Suite -->
package com.lc.testChuanCan; import org.testng.annotations.Test; public class testNG6 { //testKey参数是在test配置的,hostname是在suite里添加的都可以引用 @Test(parameters = {"testKey","hostname"}) public void textNG06_01(String testKey,String hostname) { System.out.println("我是testNG05 类的testNG06——01方法, 我传递参数是 testKey:"+testKey+" hostname:"+hostname); } }
传递参数注释也是使用下面方式
package com.lc.testChuanCan; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class testNG6 { //方式一 @Test(parameters = {"testKey","hostname"}) public void textNG06_01(String testKey,String hostname) { System.out.println("我是testNG6 类的testNG06——01方法, 我传递参数是 testKey:"+testKey+" hostname:"+hostname); System.err.println("-----------------------------"); }
//方式二 @Parameters({"testKey","hostname"}) @Test public void textNG06_02(String testKey,String hostname) { System.out.println("我是testNG6 类的testNG06——02方法, 我传递参数是 testKey:"+testKey+" hostname:"+hostname); } }
使用testNG.xml传参存在一定的局限性:
例如只能传输java的基础数据类型,
第二种方式传值 @Parameters annotation传递参数
1-使用@DataProvider(name = "reange-provider") 定义一个存储数据的方法
2-在测试方法中使用 @Test(dataProvider = "reange-provider") 引用数据。
package com.lc.testChuanCan; import java.util.HashMap; import java.util.Map; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class testNG07 { @Test(dataProvider = "reange-provider") public void testNG07_01(int n,int lower,int upper,boolean expected) { System.out.println("n:"+n+";lower:"+lower+";upper:"+upper+";expected:"+expected); } @DataProvider(name = "reange-provider") public Object[][] rangeData(){ int lower = 5 ; int upper = 10 ; return new Object[][] { {lower-1,lower,upper,false}, {lower,lower,upper,true}, {lower+1,lower,upper,true}, {upper,lower,upper,true}, {upper+1,lower,upper,false}, }; } }