TestNG能够经过testng.xml和Data Providers向测试方法传递参数java
利用testNG.xml传递参数ide
1-建立一个TestNG测试类测试
其中 parameters = {"xml-file","hostname"} 使用来接受参数的,由于有两个值因此有两个参数ui
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方法,\n我传递参数是\nxml-file:"+xmlfile+"\nhostname:"+hostname); } }
2-建立一个TestNG.xml文件spa
设置参数标签code
<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
<?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方法,\n我传递参数是\ntestKey:"+testKey+"\nhostname:"+hostname); } }
传递参数注释也是使用下面方式blog
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方法,\n我传递参数是\ntestKey:"+testKey+"\nhostname:"+hostname); System.err.println("-----------------------------"); }
//方式二 @Parameters({"testKey","hostname"}) @Test public void textNG06_02(String testKey,String hostname) { System.out.println("我是testNG6 类的testNG06——02方法,\n我传递参数是\ntestKey:"+testKey+"\nhostname:"+hostname); } }
使用testNG.xml传参存在必定的局限性:it
例如只能传输java的基础数据类型,若是想传送其余的数据类型就不行了例如mapio
因此使用第二种方式传值 @Parameters annotation传递参数