1 Introduction
Now this, we learn TestNG.xml file in front of us already know, TestNG is to run this file to execute test cases. By manual carefully you can further understand: This file is configured test cases, test suites. In simple terms, the use of this document, we can run the same class or different classes inside more test cases.
TestNG by setting testng.xml file can do the following things
1) Create a test suite sources of different packages, classes, methods
2) comprises a number of options, for example, the failure may be re-run with the embodiment.
3) support the use of regular expressions
4) Run the external parameters passed test methods
5) supports the configuration of multi-threaded execution environment
testNG Inspired by a testing framework JUnit NUnit and by making use of testNG test easier. Such as the following features:
1. to manage the test by comments
2. The concurrent execution of multi-threaded test, and is safe
3. Support data-driven testing
4. When used on a variety of platforms (Eclipse, IDEA, Maven, etc.)
The embedded BeanShell, so that the flexibility
6. Support dependence
The structure of the xml file 2.testNG
2.1 is probably a simple structure is as follows:
<suite> --<test> -- --<parameter> -- --<test> -- -- -- <parameter> -- -- -- <groups> -- -- -- <classes>
2.2 Comparison of the detailed structure is as follows:
<suite> <suite-files> <suite-file path=""></suite-file> </suite-files> <parameter name="" value=""></parameter> <method-selectors> <method-selector> <selector-calss name=""></selector-calss> </mehod-selector> </method-selectors> <test name=""> <parameter name="" value=""><parameter> <groups> <define name=""> <include name=""/> <exclude name=""/> </define> <run> <include name=""/> <exclude name=""/> </run> </groups> <classes> <class name=""> <mehods> <parameter name="" value=""></parameter> <include name=""></include> <exclude name=""></exclude> </methods> </class> <class></class> </classes> <packages> <package name=""> <include name=""></include> <exclude name=""></exclude> </package> </packages> <listeners> <listener class-name=""/> </listenters> </test> <test></test> </suite>
3. Document the specific Element Description
3.1<suite>
testNG.xml outermost tag file, i.e. suite
, that the test kit, which may have a plurality of lower <test>
and <groups>
that there are several properties that can be added
Description: an xml file can have only one <suites> ,, is a root level xml file
<Suite> a <test> and <parameters> Composition
Parameter Description:
parameter | Explanation | Instructions | Parameter Value |
name | Required, <suite> name will appear in the reports in | name="XXX" | suite name |
junit | Whether Junit pattern (recognition setup (), etc.) | junit="true" | true and false, default false |
verbose | Details of the output level of the console, on a 0-10 scale (0 no 10 most detailed) | verbose="5" | 0-10 |
parallel | We are tested in different threads in parallel, to use the thread-count package | parallel="mehods" | Contents shown in the table, default false |
parent-module | And Guice framework for run only once, create a parent injector to all guice injectors | ||
guice-stage | And Guice framework for | guice-stage="DEVELOPMENT" | DEVELOPMENT,PRODUCTION,TOOL,默认"DEVELOPMENT" |
configfailurepolicy | After the test fails again whether to perform or skip, skip value and continue | configfailurepolicy="skip" | skip, continue, default skip |
thread-count | And supporting the use of parallel thread pool size, determines the number of parallel threads | thread-count="10" | Integer, default 5 |
annotations | Gets notes, when the value of javadoc, use JavaDoc comments; otherwise annotated with JDK5 | annotations="javadoc" | javadoc |
time-out | When disposed parallel, termination waiting time (ms) prior to the execution unit | time-out="10000" | Integer in milliseconds |
skipfailedinvocationcounts | Whether to skip the call failed | skipfailedinvocationcounts="true" | true and false, default false |
data-provider-thread-count | The number of data-provider concurrent thread pool | data-provider-thread-count="5" | Integer |
object-factory | IObjectFactory interface implement a class, instantiates the test object | object-factory="classname" | The class name |
allow-return-values | Allow to return function value | all-return-values="true" | true and false |
preserve-order | Whether sorted | preserve-order="true" | true and false, default true |
group-by-instances | Packet according to Example | group-by-instances="true" | true and false, default false |
False value of the parameter, methods, tests, classes, instances. Default false
parallel and thread-count must be supporting the use of, or the equivalent of invalid parameters, thread-count determines the number of threads open when parallel test
parallel = "mehods" TestNG parallel execution of all test methods in a different thread
parallel="tests" TestNG将并行执行在同一个<test>下的所有方法在不同线程里
parallel="classes" TestNG将并行执行在相同<class>下的方法在不同线程里
parallel="instances" TestNG将并行执行相同实例下的所有方法在不同的县城里
parent-module和guice-stage和Guice框架有关,testNG 6对Guice框架提供了支持,我没用过这个框架,所以这两个参数没看懂╮(╯▽╰)╭
3.2<suite-file>
说明:引入外部的xml文件(地址由path参数决定,path必填项),将引入的xml与当前的xml文件一起使用
声明方法:
<suite-files> <suite-file path="/path/suitefile1"></suite-file> </suite-files>
3.3<test>
此标签无特别意义,其下可以包括多个标签,如groups
、classes
等
说明:一个<suite>下可以有多个<test>,可以通过<suite>的parallel="tests"来进行并行测试,必须和thread-count配套使用,否则是无效参数
<test>由<parameters>、<groups>、<classes>三部分组成
参数说明:
参数 | 说明 | 使用方法 | 参数值 |
name | test的名字,将出现在报告里 | name="testname" | test的名字 |
junit | 是否按照Junit模式运行 | junit="true" | true和false,默认false |
verbose | 控制台输出的详细内容等级,0-10级(0无,10最详细),不在报告显示 | verbose="5" | 0到10 |
parallel | 是否在不同的线程并行进行测试,要与thread-count配套使用 | parallel="mehods" | 与suite的parallel一致,默认false |
thread-count | 与parallel配套使用,线程池的大小,决定并行线程数量 | thread-count="10" | 整数,默认5 |
annotations | 获取注解,值为javadoc时,使用JavaDoc的注释;否则用JDK5注释 | annotations="javadoc" | javadoc |
time-out | 设置parallel时,终止执行单元之前的等待时间(毫秒) | time-out="10000" | 整数,单位毫秒 |
enabled | 标记是否执行这个test | enabled="true" | true和false,默认true |
skipfailedinvocationcounts | 是否跳过失败的调用 | skipfailedinvocationcounts="true" | true和false,默认false |
preserve-order | 是否按照排序执行,如果是true,将按照xml文件中的顺序去执行 | preserve-order="true" | true和false,默认true |
allow-return-values | 是否允许返回函数值 | all-return-values="true" | true和false,默认false |
3.4<parameter>
说明:提供测试数据,有name和value两个参数
声明方法:<parameter name = "parameter_name" value = "parameter_value "/>
testng.xml文件中的<parameter>可以声明在<suite>或者<test>级别,在<test>下的<parameter>会覆盖在<suite>下声明的同名变量
3.5<method-selectors>
说明:方法选择器定义了哪些类的方法需要去执行,类必须继承自org.testng.IMethodSelector
声明方法:
<method-selectors> <method-selector> <selector-class name="classname" priority="1"></selector-class> <script language="java"></script> (language还可以用beanshell等) </method-selector> </method-selectors>
备注:<method-selectors>这个我没用过,所以了解,如果有错的欢迎指出来~
3.6<groups>
此标签必然是在<test>
标签下的,用于标识那些组会被用于测试或者被排除在测试之外,其同级必然要包含一个<classes>
标签或者<pakages>
标签,用于指定groups来自于哪些包或者类
说明:要运行的组,可以自定义一个组,可以包括要执行的,还排除要执行的方法。必须和<classes>配套使用,从下面的类中找到对应名字的方法
<groups>由<difine>和<run>、<dependencies>三部分组成。<diffine>可以将group组成一个新组,包括要执行和不执行的大组;<run>要执行的方法;<dependencies>指定了某group需要依赖的group(比如下面的例子,group1需要依赖group2和group3先执行)。
声明方法:
<groups> <define name ="all"> <include name ="testgroup1"/> <exclude name ="testgroup2'/> </define> <run> <include name ="all"/> <include name ="testmethod1"/> <exclude name="testmethod2"/> </run> <dependencies> <group name ="group1" depends-on="goup2 group3"/> </dependencies> </groups>
3.7<classes>
说明:方法选择器,要执行的方法写在这里,参数有name和priority。
注释:
1.<classes>下必须写要执行的<class>,否则不会执行任何内容,如果填写了class没有写methods,会按照填写的class的下的注释@Test去执行所有的方法
2.<classes>下的<methods>如果填写了<include>,那只会执行所填写的方法,没有填写的方法不会去执行
声明方法:
<classes> <class name="要执行的class名"> <methods> <include name ="要执行的方法名"></include> </methods> </class> </classes>
3.8<packages>
说明:<packages>指定包名代替类名。查找包下的所有包含testNG annotation的类进行测试
声明方法:
<packages> <package name="packagename"/> <package name="packagename"> <include name="methodname"/> <exclude name="methodname"/> </package> </packages>
3.9<listener>
说明:指定listeners,这个class必须继承自org.testng.ITestNGListener。在java中使用@Listeners({com.example.MyListener.class,com.example.MyMethodInterceptor.class})的注释也可以有同样效果
声明方法:
<listeners> <listener class-name="com.example.MyListener"/> <listener class-name="com.example.MyMehodIntercepor"/> </listeners>
4.小结
好了,今天关于TestNG之xml文件详解篇 ,就分享到这里。