TestNg-Ex
-
简介
TestNg-Ex主要是针对原testng做了一层扩展,目前支持的数据源有Value,Func,Csv,Enum,Spring,Json。
后续其他数据源例如DB会增加 -
依赖
JDK1.8 -
配置
- MAVEN依赖
<dependency> <groupId>com.github.phoenix-young</groupId> <artifactId>testng-ex</artifactId> <version>0.4.7</version> </dependency>
- IDE配置(以配置IDEA为例)
Run->Edit Configurations->Defaults->TestNg->Listeners下添加 com.apitest.nglisteners.ApiTestListener
-
常用
- Values数据源
package com.apitest.sample; import com.apitest.dataProvider.Value; import org.testng.annotations.Test; public class ValueProviderTest { @Test public void intValue(@Value(ints = {1,2,3}) int value){ } @Test void stringValue(@Value(strings = {"T1","T2","T3"})String value){ } }
- Enum数据源(不需要任何注解)
public enum Company { Microsoft, Sony, Nintendo; } public enum Console { XBOXONE(Company.Microsoft,2000), XBOX360(Company.Microsoft,1500), SWITCH(Company.Nintendo,2300), PS4(Company.Sony,1700), PS3(Company.Sony,1300), WII(Company.Nintendo,800), WIIU(Company.Nintendo,1300); private Company company; private int price; Console(Company company,int price){ this.company = company; this.price = price; } public Company getCompany() { return company; } public int getPrice() { return price; } } @Test public void enumInjectTest(Console console){ }
-
Method数据源
- 同类名下
public class InnerFunctionTest { @Test public void innerFuntion(@Func(name = "getValues") String value){ } public List<String> getValues(){ return Arrays.asList("A","B","C"); } }
- 静态方法
public class StaticFunctionProvider{ public static List<Integer> getValues(){ return Arrays.asList(1,2,3); } }
public class StaticFunctionTest { @Test public void staticFunction(@Func(name = "getValues",provider = StaticFunctionProvider.class) int value){ } }
- 外部实例方法(需要有无惨构造函数)
public class OuterFunctionProvider { public List<Console> getConsoles(){ return Arrays.asList(Console.values()).stream().filter(c->c.getCompany() == Company.Microsoft).collect(Collectors.toList()); } }
public class OuterFunctionTest { @Test public void outerFunction(@Func(name = "getConsoles",provider = OuterFunctionProvider.class) Console console){ } }
- 带参数的方法(方法参数只能是string类型)
public class FunctionWithParameterTest { @Test public void functionWithParameter(@Func(name = "getData",args = {"5","2"})int value){ } public List<Integer> getData(String p1,String p2){ int value = Integer.parseInt(p1); List<Integer> list = new ArrayList<>(); for(int i =0;i<value;i++){ list.add(i); } return list; } }
-
CSV数据源
-
id name age 1 test1 22 2 test2 18 3 test3 19 csv数据 (CSV文件需要和测试类放在同一PACKAGE下)
id name age 1 test1 22 2 test2 18 3 test3 19 -
CSV转化成POJO
public class Student implements Serializable { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
public class csvDataProvider { /** * Csv不指定File默认为当前类名同名CSV文件 * 此例为csvDataProvider.csv * @param student */ @Test public void csvData(@Csv Student student){ } /** * 指定csv文件名 */ @Test public void csvDataWithFileName(@Csv(files = "csvDataProvider.csv") Student student){ } /** * 用MAP类型代替POJO */ @Test public void csvDataUsingMap(@Csv Map<String,String> student){ } }
-
-
组合(每个参数可以理解为一个维度,多个参数组合就生成笛卡尔积,针对POJO对象需要实现 Serializable 接口)
public class CombinationProviderTest {
/**
* console有7个枚举值,company有3个枚举值
* 最终会生成7*3=21组测试数据
*/
@Test
public void enumCombinationTest(Console console, Company company){
}
/**
* console有7个枚举值,value有3组数字,str有两组字符串
* 最终会生成 7*3*2=42组测试数据
*/
@Test
public void otherCombinationTest(Console console, @Value(ints = {1,2,3}) int value,@Value(strings = {"a","b"}) String str){
}
}
- 过滤(过滤掉不需要的组合)
package com.apitest.sample.combination;
import com.apitest.annotations.Filter;
import com.apitest.testModels.Company;
import com.apitest.testModels.Console;
import org.testng.annotations.Test;
public class CombinationWithFilterTest {
/**
* console有7个枚举值,company有3个枚举值
* 过滤器只需要company == Company.Microsoft
* 最终生成7*1=7组测试数组
*/
@Test
@Filter(method = "filterCompany")
public void filterTest(Console console, Company company){
}
/**
* 注意事项
* 1. 过滤方法的参数类型和个数必须与测试方法一致
* 2. 过滤方法的返回类型一律为布尔类型
*/
public boolean filterCompany(Console console,Company company){
return company == Company.Microsoft;
}
public boolean filterCompany2(Console console,Company company){
return console == Console.PS4 || console == Console.SWITCH;
}
/**
* 由于有两组过滤器
* 最终生成console == Console.PS4 || console == Console.SWITCH 并且 company == Company.Microsoft 两组测试数据
*/
@Test
@Filter(method = "filterCompany")
@Filter(method = "filterCompany2")
public void filterTest2(Console console,Company company){
}
}
网友评论