参数化有两种方法:
第一种:在xml文件中声明
第二种:用@DataProvider注解
ParameterTest类:用@Parameters({"name","age"}) 注解声明参数
package com.janson.parameter;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParameterTest {
@Test
@Parameters({"name","age"})
public void paramTest(String name,int age) {
System.out.println("name:"+ name);
System.out.println("age:"+age);
}
}
在resources文件夹下新建parameter.xml文件:
注:<parameter>标签中的name需要与上面类中的@Parameters({"name","age"}) 一致
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="parameter">
<test name="parameterTest">
<parameter name="name" value="janson"/>
<parameter name="age" value="22"/>
<classes>
<class name="com.ucar.parameter.ParameterTest"/>
</classes>
</test>
</suite>
运行结果如下:
name:janson
age:22
第二种方法:
package com.janson.parameter;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.lang.reflect.Method;
public class DataProviderTest {
//演示两个参数
@Test(dataProvider = "twoParam")
public void dataProviderTest(String name,int age) {
System.out.println("name = "+name+";age = "+age);
}
@DataProvider(name = "twoParam")
public Object[] provideData() {
Object[][] o = new Object[][] {
{"zhangsan",10},
{"lisi",11},
{"wangwu",12},
{"zhaoliu",13}
};
return o;
}
//演示一个参数
@Test(dataProvider = "OneParam")
public void onlyOneDataProvider(int money) {
System.out.println("my Money is:"+ money+"万");
}
@DataProvider(name="OneParam")
public Object[] oneProvideData() {
Object[] o = new Object[]{
100,200,1000,2000
};
return o;
}
//演示多个参数
@Test(dataProvider = "manyParam")
public void manyDataProvider(String name,int age,String nation,int height) {
System.out.println("name:"+name+",age:"+age+",nation:"+nation+",height:"+height);
}
@DataProvider(name = "manyParam")
public Object[][] manyProvideData() {
Object[][] o = new Object[][] {
{"A",20,"中国",180},
{"B",21,"USA",150},
{"C",22,"AUS",160},
{"D",23,"CAN",170}
};
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[][] provideMethodData(Method method) {
Object[][] result = null;
if(method.getName().equals("test1")) {
result = new Object[][] {
{"ZhangSan",30},
{"LiSi",31}
};
}else if(method.getName().equals("test2")) {
result = new Object[][] {
{"WangWu",40},
{"ZhaoLiu",41}
};
}
return result;
}
}
执行结果如下:
---两个参数---
name = zhangsan;age = 10
name = lisi;age = 11
name = wangwu;age = 12
name = zhaoliu;age = 13
---多个参数---
name:A,age:20,nation:中国,height:180
name:B,age:21,nation:USA,height:150
name:C,age:22,nation:AUS,height:160
name:D,age:23,nation:CAN,height:170
---一个参数---
my Money is:100万
my Money is:200万
my Money is:1000万
my Money is:2000万
---根据不同方法传入不同参数---
test1方法: name=ZhangSan,age=30
test1方法: name=LiSi,age=31
test2方法: name=WangWu,age=40
test2方法: name=ZhaoLiu,age=41
补充:如果被引用数据源在另一个测试类中定义的,还需要指明其所在测试类,示例如下:
@Test(dataProvider = "create", dataProviderClass = StaticProvider.class)
public void test(Integer n) {
// ...
}
网友评论