testNg测试框架搭建
设计背景
为了方便sdk开发程序包的测试,搭建testNg测试框架,为了能够更加方便的编写sdk的测试用例,以及提升测试的代码覆盖率。本期主要是完成sdk测试模块,后续也会增加接口测试模块。
设计思路:
主要设计师测试程序和测试用例的分离。
测试用例编写在项目代码中,使用testNg对应的注解完成测试用例;测试数据是存放在项目外,通过绝对地址获取测试用例的数据。本项目主要支持csv,xml,json三种格式的测试用例的数据结构。
TestNg简介
一、TestNg 介绍:
TestNg 优势:
1、比Junit 涵盖的功能更全面的测试框架
2、Junit 更适合隔离性比较强的单元测试
3、TestNg更适合复杂的集成测试
二、注解
BeforeMethod and AfterMethod
每个测试用例之前都会运行
BeforeClass and AfterClass
类之前运行的方法
BeforeSuit and AfterSuit
测试套件 在 BeforeClass 之前运行
运行顺序:
BeforeSuit–BeforeClass–BeforeMethod-- case
三、异常测试
什么时候会用到异常测试?
在我们期望结果为某一个异常的时候 就要用到异常测试
比如:我们传入了某些不合法的参数 ,程序会抛出异常,也就是说预期结果就是一个异常
resources : 是一个放配置文件的文件夹
四:忽略测试
某些不需要执行的测试 可以忽略 不执行 可以加属性:Test(enable = false)
五:超时测试
某些测试如果响应的时间超过多少秒 就抛出超时的异常 可以加属性 Test(timeout = 3000) 单位是毫秒
Thread.sleep(mills = 3000) 线程休眠
六、依赖测试
某一个方法执行要依赖前一个方法的执行 就叫依赖
Test(dependsOnMethods = {‘被依赖的方法名’})
被依赖的方法报错了,需要依赖的方法就会被忽略掉
七、参数化测试:
1、xml 文件参数化
2、DateProvider 参数化
testNg搭建项目过程
首先,在pom.xml中添加testng和reportng相关依赖
<dependencies>
<!-- 添加testNG依赖 -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<!-- <scope>test</scope>-->
</dependency>
<!-- 添加reportNG依赖 -->
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.2</version>
<scope>test</scope>
<!-- 排除testNG依赖 -->
<exclusions>
<exclusion>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
</dependencies>
pom.xml中配置maven-surefire-plugin并加入reportng listener
<properties>
<xmlFileName>testng.xml</xmlFileName>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<skipTests>false</skipTests>
<suiteXmlFiles>
<suiteXmlFile>${xmlFileName}</suiteXmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter</value>
</property>
</properties>
<forkMode>always</forkMode>
</configuration>
</plugin>
</plugins>
</build>
在testng.xml中标签加入listener
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter"/>
<listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
</listeners>
在Idea中打开Run-Edit Configurations…
在Listeners标签下勾选“Use default reporters”
最后运行testng.xml,自动生成test-output目录,在html目录下找到index.html
使用demo
说明:执行具体类适合调试具体的测试程序,执行多个测试程序生成对应的测试报告需要使用xml运行测试程序
一、csv测试用例
部分代码:
@Test(dataProvider = "dealsQueryData")
public void getDealsQuery(DealsQuery dealsQuery) {
System.out.println("getDealsQuery");
System.out.println(JSONObject.toJSONString(dealsQuery));
}
@DataProvider(name = "dealsQueryData")
public Object[] ProviderData() {
String path = "E:\\csvtest.csv";
List<DealsQueryModel> dealsQueryModels = ToJavaBeanUtil.toJavaBeans(path, DealsQueryModel.class, DataType.CSV);
List<DealsQuery> dealsQueries = dealsQueryModels.stream().map(p -> {
DealsQuery dealsQuery = converter(p);
return dealsQuery;
}).collect(Collectors.toList());
return dealsQueries.toArray();
}
通过绝对路径获取测试用例的数据,传入到具体的测试方法,进入到具体的测试程序执行测试方法。
二、Xml测试用例
@Test(dataProvider = "dealsQueryData")
public void getDealsQuery(DealsQuery dealsQuery) {
System.out.println("getDealsQuery");
System.out.println(JSONObject.toJSONString(dealsQuery));
}
@DataProvider(name = "dealsQueryData")
public Object[] ProviderData() {
String path = "E:\\xmltest2.xml";
DealsQueryXmlModels dealsQueryXmlModels =ToJavaBeanUtil.toJavaBean(path, DealsQueryXmlModels.class,DataType.XML);
List<DealsQuery> dealsQueries = dealsQueryXmlModels.getDealsQueryXmlModel().stream().map(p -> {
DealsQuery dealsQuery = converter(p);
return dealsQuery;
}).collect(Collectors.toList());
return dealsQueries.toArray();
}
三、Json测试用例
@Test(dataProvider = "dealsQueryData")
public void getDealsQuery(DealsQuery dealsQuery) {
System.out.println("getDealsQuery");
System.out.println(JSONObject.toJSONString(dealsQuery));
}
@DataProvider(name = "dealsQueryData")
public Object[] ProviderData() {
String path = "E:\\jsontest.json";
List<DealsQueryJsonModel> dealsQueryXmlModelList = ToJavaBeanUtil.toJavaBeans(path, DealsQueryJsonModel.class, DataType.JSON);
List<DealsQuery> dealsQueries = dealsQueryXmlModelList.stream().map(p -> {
DealsQuery dealsQuery = converter(p);
return dealsQuery;
}).collect(Collectors.toList());
return dealsQueries.toArray();
}
四、测试报告
1.测试单个类
测试报告
2.测试多个类
测试报告
备注
1.解决不同用例之间的依赖关系
通过testNg自带的注解可一解决用例之间的依赖问题
2.多线程并发的问题
不同dataprovider的并发:
默认情况下,dp并行执行的线程池容量为10,如果要更改并发的数量,也可以在suite tag下指定参数data-provider-thread-count:
同一个方法的并发:
@Test(enabled=true, dataProvider=“testdp”, threadPoolSize=5, invocationCount=10)
其中threadPoolSize表明用于调用该方法的线程池容量,该例就是同时起5个线程并行执行该方法;invocationCount表示该方法总计需要被执行的次数。该例子中5个线程同时执行,当总计执行次数达到10次时,停止。
项目git地址:https://github.com/why154285/testNgtest.git