TestNG扩展
1. TestNG API
本章节将讨论如何使用TestNG API来创建自己的TestNG对象。TestNG的API基本由接口组成,这样做是为了容易模拟TestNG返回的对象。
1.1 org.testng.TestNG、ITestResult、ITestListener、ITestNGMethod
TestNG类是TestNG的的主入口,它让我们指定测试类,指定包含的分组或排除的分组,指定要执行的XML文件等。
public static void mian(String[] args){
TestNG tng = new TestNG();
tng.setTestClasses(new Class[]{ MyTest.class});
TestListenerAdapter listener = new TestListenerAdapter();
tng.addListener(listener);
tng.run();
log("PASSED:" + listener.getPassedTests().size());
}
这里TestListenerAdapter类提供了ITestListener接口的一个简单实现。这里使用TestListenerAdapter的另一项功能:记录所有通过的测试案例数量。
通过TestListenerAdapter可以方便的获取结果的程序清单,如果需要为这个类添加功能,又保留测试结果程序清单,则可以调用被覆盖方法的super()方法,如下所示:
public class MyTestListener extends TestListenerAdapter{
public void onTestSuccess(ITestResult result){
super.onTestSuccess(result);
//do you own processing
}
}
TestListenerAdapter 保存了测试结果:
public List<ITestResult> getPassedTests()
public List<ITestResult> getFailedTests()
public List<ITestResult> getSkippedTests()
ITestResult 接口能让我们获取某个测试状态、测试方法、参数,以及开始时间和结束时间:
public void onTestSuccess(ITestResult iTestResult) {
long time =
(iTestResult.getEndMillis() - iTestResult.getStartMillis());
log("Success, method:" + iTestResult.getMethod()
+" #parameters:"+iTestResult.getParameters().length
+ " time:" + time);
}
ITestResult# getResult 返回一个ITestNGMethod,这个是TestNG对测试方法的一个视图。从这个对象我们可以获得TestNG对象当前调用的原始java.lang.reflact.Method, 并获得其他信息,如:
- 分组/方法信息(这个方法所属的分组,它依赖的分组和方法)。
- 调用的次数,线程池的大小以及超时设置。
- 这个方法是否是一个配置方法(@Before/@After), 它是哪一种配置方法:
ITestNGMethod method = iTestResult.getMethod();
log(" Method:"+method.getMethodName()
+" invocationCount:" +method.getInvocationCount()
+" #groups:"+method.getGroups().length
+" timeOut:"+method.getTimeOut());
1.2 XML API
TestNG提供了一个简单的接口,让我们访问自己的testng.xml, 甚至可以从头创建它。与XML API相关的这些类在org.testng.xml中,每个XML标签都有一个对应的类:
标签 |
类名 |
<suite> | XmlSuite |
<test> | XmlTest |
<package> | XmlPackage |
<class> | XmlClass |
<method-selector> | XmlMethodSelector |
xml 文件如下:
<suite name="testng" verbose="1" thread-count="2">
<parameter name="first-name" value="cedric" />
<test name="Regression 1">
<group>
<run>
<exclude name="excludeThisGroup"/>
</run>
</group>
<classes>
<class name="test.parameter.test1" />
<class name="test.parameter.test2" />
</classes>
</test>
</suite>
XmlSuite的值:
方法 | 返回结果 |
getName() | testng |
getVerbose() | true |
getThreadCount() | 2 |
getParameters() | Map<String, String>:{“first-name”=>”cedric”} |
geTests() | List<XmlTest> |
XmlTest的值:
方法 | 返回结果 |
getName() | Regression1 |
getIncludeGroups() | {} |
getExcludeGroups() | List<String>:{“excludeThisGroup”} |
getXmlClasses | List<XmlClasses>:{ “test.parameter.test1”, ”test.parameter.test2” } |
geTests() | List<XmlTest> |
1.3
生成xml文件
XmlSuite suite = new XmlSuite();
suite.setName("TestNG");
suite.setVerbose(1);
suite.setThreadCount(2);
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("first-name", "credic");
suite.setParameters(parameters); XmlTest test = new XmlTest(suite);
test.setName("regression1");
test.setExcludedGroups(Arrays.asList(new String[]{"excludedGroup"})); XmlClass[] classes = new XmlClass[]{
new XmlClass("test.parameters.test1"),
new XmlClass("test.parameters.test1"),
};
test.setXmlClasses(Arrays.asList(classes); TestNG tng = new TestNG();
//指定xml路径方式
tng.setTestSuites(Arrays.asList(new String[]{
"testng.xml","test-15/testng.xml"}))
//使用当前生成的文件
tng.setXmlSuites(Arrays.asList(new XmlSuite[]{ suite }));
tng.run();
2. 方法选择器:
Testng通过方法选择器(IMethodSelector)来决定在执行测试时,包含或排除哪些方法。
TestNG的默认方法选择器:XmlMethodSelector,它会根据testng.xml中的内容来实现决定逻辑,它的优先级为10.
- 如果希望你自己的选择器先调用,则可以把优先级设为0~9.
- 如果先考虑testng.xml, 则把优先级设置为大于10.
- 如果希望自己的方法选择器替换所有默认的方法,将优先级设置为负数。在这种情况下只有带负数优先级的方法选择器会生效。
- 方法选择器直接是OR的关系:即一个方法选择器对某个测试方法返回true,则这个测试方法就会包含在这次执行中,否则,具有下一个优先级的方法选择器会被调用(0排在10前面)。如果没有方法选择器则返回true,这个测试方法不会执行。
3 annotation转换器(annotationTransformer)
用户实现annotation转换器,是为了覆盖TestNG在运行时看到的annotation。
public void InvocationTransform implements IAnnotationTransformer{
public void transform(ITest test, class cls, Constructor con, Method method){
if("two".equals(method.getName()){
test.setInvocationCount(2);
}else if("three".equals(method.getName()){
test.setInvocationCount(3);
}
}
}
其他用法:
- timeout:修改测试方法的超时设置;
- enabled:修改 @Test annotation上的enabled标识。
- invocationCount:用于多线程环境或负载测试。
- threadPoolSize:annotation可以查询本地信息(处理器个数,可用堆大小或计算机负载),然后增加或减少线程池的大小。
- successPercentage:和invocationCount一起使用,允许一定的失败比例。
- dataprovider:在运行时修改数据提供者的名称。用于数据提供者本身执行动态判断。
- description:用于HTML报告生成。
- group:可以进行基于环境的变量分组调整。
- dependsOnGroups/dependsOnMethod/alwaysRun:这些属性会直接影响执行顺序,请尽量避免使用annotation转换器来修改他们。
4. 报告api
4.1 默认报告:
默认报告生成在./test-output目录下
4.2 报告api
- 利用org.testng.Reporter类,向默认报告添加定制信息。
- ITestListener 是一个实现了org.testng.ITestListener接口的类,其中的onStart()和onFinish() 分别在testSuite开始和结束时被调用。onTest*开头的方法在测试方法被执行时调用。
- IReporter 实现了org.testng.IReporter接口
TestNG扩展的更多相关文章
- 收藏清单: python测试框架最全资源汇总
xUnit frameworks 单元测试框架 frameworks 框架 unittest - python自带的单元测试库,开箱即用 unittest2 - 加强版的单元测试框架,适用于Pytho ...
- 转 python测试框架最全资源汇总
转自: http://www.testclass.net/list/python_list_1/ xUnit frameworks(单元测试框架) frameworks 框架 unittest - p ...
- python测试框架&&数据生成&&工具最全资源汇总
xUnit frameworks 单元测试框架frameworks 框架unittest - python自带的单元测试库,开箱即用unittest2 - 加强版的单元测试框架,适用于Python 2 ...
- APP接口自动化测试JAVA+TestNG(二)之TestNG简介与基础实例
前言 继上篇环境篇后,本篇主要对TestNG进行介绍,给出最最基础的两个实例,通过本文后,学会并掌握TestNG测试用例的编写与运行,以及生成美化后的报告.下一篇为HTTP接口实战(国家气象局接口自动 ...
- TestNG
一.TestNG 是什么 ? 脱胎于业界标杆的Junit,并超于Junit,主要原因是由于当时的JUnit3版本不支持annotation,使用不够灵活. TestNG不再需要test前缀的命名方式. ...
- selenium第一课(selenium+java+testNG+maven)
selenium介绍和环境搭建 一.简单介绍 1.selenium:Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包 ...
- (转)testng对失败时截图处理
写这篇微博之前,自动化测试代码中有对于失败测试截图的功能,但是却散落在各个catch语句块中.不便于以后的扩展和维护,AOP思想里说明是面向切面编程,把公共的组件提取出来,可以单独修改维护. 但是直到 ...
- testng参数化(提供测试数据)【转】
testng提供测试数据的两个注释:@DataProvide和@Parameter 一.通过testng.xml中设置参数 (实际上testng.xml只是一个名字,可以起任何一个名字,只要是.x ...
- TestNG 三 测试方法
一.设置参数 测试方法是可以带有参数的.每个测试方法都可以带有任意数量的参数,并且可以通过使用TestNG的@Parameters向方法传递正确的参数. 设置方式有两种方法:使用testng.xml或 ...
随机推荐
- C#中的四舍五入算法
最近在产品开发过程中遇到一个问题,就是在对数值进行截取,例如说保留两位小数时,最终得到的结果跟预期的在某些情况下会产生差异,这个差异的表现就是最后一位与预期的不一致,也就是说在"四舍五入&q ...
- 排序算法简介及其C实现
排序算法(Sorting Algorithm)是计算机算法的一个组成部分. 排序的目标是将一组数据 (即一个序列) 重新排列,排列后的数据符合从大到小 (或者从小到大) 的次序.这是古老但依然富有挑战 ...
- [JIT_APP]Activity生命周期相关的7个方法
先发一张安卓官方文档里面的Activity生命周期图解 下面在对这7个生命周期内相关的方法做一些简单的介绍 OnCreate() 当Activity被创建的时候,会自动运行该方法.该方法做一些初始化动 ...
- n皇后问题leetcode-51. N-Queens
n皇后问题是应用回溯法的经典问题.任一行.列.对角线不能有两皇后并存,因此在判断是否合法时,可以将某一行是否有皇后.某一列是否有皇后分别用数组存起来.注意到,对于往左下右上的对角线,每个点的行号(i) ...
- UCloud EIP 你真的懂得如何使用么? - SegmentFault
UCloud EIP 你真的懂得如何使用么? - SegmentFault UCloud EIP 你真的懂得如何使用么?
- 【转】VS2013编译libjpeg库
原文地址:http://blog.csdn.net/weixinhum/article/details/42718959 现在,很多图像处理工具和开源库都给出了图像解码的函数接口,然而有时这些接口并不 ...
- java 检查抛出的异常是否是要捕获的检查性异常或运行时异常或错误
/** * Return whether the given throwable is a checked exception: * that is, neither a RuntimeExcepti ...
- .Net设计模式_单列模式
理解 博友的经典说法:很多人排队去厕所蹲坑一样,每一次只能让一个人去蹲坑,这是一种通俗的理解. 理论上的理解则为,我们需要写一个类,这个类的作用就是控制,从而保证在整个应用程序的生命周期中,在任何时刻 ...
- spring mvc 介绍
Spring MVC Tutorial tag. * * If you do not want to deal with the intricities of the noscript * secti ...
- java.lang包
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1.特性——不用import 2.String String x = "abc"; < ...