1、TestNG官网:TestNG - Welcome
2、testNG是测试框架 ,旨在简化广泛的测试需求,从单元测试到集成测试都可以使用,同时testNG集成了junit和nunit,使得它配置更加的灵活;testNG新增的功能有注解、参数化、数据驱动、多线程运行等主要功能
3、安装配置参照:Eclipse安装TestNG插件
4、特性:
- Annotations. 注解
- Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc...). 线程池
- Test that your code is multithread safe.
- Flexible test configuration.
- Support for data-driven testing (with @DataProvider). 数据驱动
- Support for parameters. 参数化
- Powerful execution model (no more TestSuite).
- Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).
- Embeds BeanShell for further flexibility.
- Default JDK functions for runtime and logging (no dependencies).
- Dependent methods for application server testing.
总结:灵活配置,注解多线程安全参数化,数据驱动
以下三步来实现以上特性
1、Write the business logic your test and insert TestNG annotations in your code
2、Add the information about your test (e.g.the class name,the groups you wish to run,etc...)in a testng.xml file or in build.xml
3、Run TestNG
4、TestNG pom.xml配置:
<!-- testng配置 -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
二、testNG
1、Write the business logic your test and insert TestNG annotations in your code
操作:sre/test/java 创建testNG类---引入A、B
注:当testng class文件建立完成后,即第1,2步可完成,余下需继续完善第1,2步
2、Add the information about your test (e.g.the class name,the groups you wish to run,etc...)in a testng.xml file or in build.xml
1、执行方式
基于类文件执行 好处:指定某个类文件即执行某个类文件;不足:当包下类文件比较多,用此方法效率比较低
2、基于包执行
3、类文件-include/exclude方式
总结:
@Test用例不想执行的方法有哪些
①、Test(enable=false)
②、testng.xml配置include或exclude
③、代码注释
4、parallel 4种设置 + thread-count=“n”
methods tests class instances
类中所有方法 @Test 类 类的实例
注:invocationCount和thread-count选一种,是否用多线程执行要根据业务用例来定,当前接口自动化就不需要多线程
5、Run TestNG
默认报告:autotest\test-output\index.heml- 基础信息都有,但不美观,缺少数据统计
优化报告:引入Reporter
配置: pom.xml配置(reportng,guice) 见testng.xml具体配置--reportng
注:reportng 开源维护到1..1.4版本 报告地址:\aututest\test\test-output\html\index.html
guice是测试报告UI界面
优化:testng.xml配置 -- TestRun.java(增加Report.log)
reportng.jar 继续优化 出现圆饼图
三、详解--testng类文件:
1、注解相关
@DataProvider:官方文档
name:①.属性设置name="xx",@Test(dataProvider=“xx”)
②.属性不设置name,@Test(dataProvider=“方法名”
parallel 设置true 需在testng.xml的parallel = false后加 data-provider-thread-count="?"
@Test:官方文档
dataProvider 对应上面两周情况,@Test自己会循环每行数据
Object[][]有n列就需要有n个入参对应
dataProviderClass 总结:当多个数据驱动出现同时后,可以把数据驱动封装一个class中,结构清晰,下边的测试类调用时,可直接调用这个类
方法:
groups = {"init"}
groups = {"init"}
groups = {"init"}
方法dependsOnGroups = {"init.*"} 格式:解决依赖单个组,依赖多个租(同一名字组即init.*,或其他组)
A1 @test
A2 @test
A3 @test
B方法@test(dependsOnMethods = {“A1”,“A2”}),解决依赖单个方法/多个方法
解决测试用例依赖。相同点:解决依赖问题。不同点:使用方式
解决执行效率:
invocationCount 方法被执行的次数
invocationTimeOut 必须和invocationCount一起用,规定时间完成,否则超时报错
threadPoolSize 与invocationCount有关
successPercentage 用例单执行1次没必要设置,当设置invocation时刻设置此属性
timeout 平时用timeout限制用例执行时间
singleThreaded
@test(invocationCount=100,invocationTimeOut=1000,successPercentage=99,threadPoolSize=4)
测试用例优先级和开关
priority
enabled
三、注解执行顺序
何时使用注解:
@beforeSuite:
@beforeTest
@beforeClass
@DataProvider
@beforeMethod
@test
@afterMethod
@beforeMethod
@test
@afterMethod
@AfterClass
@AfterTest
@AfterSuite
代码优化
代码中Excel文件路径是固定的?
解决思路:testng参数化
testng.xml 进行<parameter name= "filePathParam" value="xx"/>配置