TestNG使用说明Word格式文档下载.docx
- 文档编号:6301717
- 上传时间:2023-05-06
- 格式:DOCX
- 页数:51
- 大小:310.96KB
TestNG使用说明Word格式文档下载.docx
《TestNG使用说明Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《TestNG使用说明Word格式文档下载.docx(51页珍藏版)》请在冰点文库上搜索。
离线安装
首先下载testng离线包:
1.将解压后的文件..\eclipse-testng离线包\features\org.testng.eclipse_6.9.9.201510270734 文件夹 放到 eclipse--》features目录下
2.将解压后的文件..\eclipse-testng离线包\plugins\org.testng.eclipse_6.9.8.201510130443 文件夹 放到eclipse--》plugins目录下
3.重启eclipse
4.验证是否安装成功,file-->
new-->
other-->
TestNg
Jar包下载地址
下载地址:
TestNG的官方文档原文请见http:
//testng.org/doc/documentation-main.html
Annotation注释
@BeforeSuite
@AfterSuite
@BeforeTest
@AfterTest
@BeforeGroups
@AfterGroups
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
ConfigurationinformationforaTestNGclass:
@BeforeSuite:
被注释的方法将在所有测试运行前运行
@AfterSuite:
被注释的方法将在所有测试运行后运行
@BeforeTest:
被注释的方法将在测试运行前运行,此方法在<
标记将要运行前。
@AfterTest:
被注释的方法将在测试运行后运行
@BeforeGroups:
被配置的方法将在列表中的gourp前运行。
这个方法保证在第一个属于这些组的测试方法调用前立即执行。
@AfterGroups:
被配置的方法将在列表中的gourp后运行。
这个方法保证在最后一个属于这些组的测试方法调用后立即执行。
@BeforeClass:
被注释的方法将在当前类的第一个测试方法调用前运行。
@AfterClass:
被注释的方法将在当前类的所有测试方法调用后运行。
@BeforeMethod:
被注释的方法将在每一个测试方法调用前运行。
@AfterMethod:
被注释的方法将在每一个测试方法调用后运行。
alwaysRun
对于每个bufore方法(beforeSuite,beforeTest,beforeTestClass和beforeTestMethod,但是不包括beforeGroups):
如果设置为true,被配置的方法将总是运行而不管它属于哪个组。
对于after方法(afterSuite,afterClass,...):
如果设置为true,被配置的方法甚至在一个或多个先调用的方法失败或被忽略时也将运行。
dependsOnGroups
这个方法依赖的组列表
dependsOnMethods
这个方法依赖的方法列表
enabled
这个类的方法是否激活
groups
这个类或方法所属的分组列表
inheritGroups
如果设置为true,这个方法被属于在类级别被@Testannotation指定的组
@DataProvider
标记一个方法用于为测试方法提供数据。
被注释的方法必须返回Object[][],其中每个Object[]可以指派为这个测试方法的参数列表。
从这个DataProvider接收数据@Test方法需要使用一个和当前注释相同名称的dataProvider名称
name这个DataProvider的名称
name
这个DataProvider的名称。
如果未定义,dataprovider将自动命名为方法名。
parallel
如果设置为true,测试中的dataprovider会同时运行。
默认值为false。
@Factory
标记方法作为一个返回对象的工厂,这些对象将被TestNG用于作为测试类。
这个方法必须返回Object[]。
@Listeners
在一个测试类中定义监听器。
value
一串数组类扩展org.testng.ITestNGListener.
@Parameters
描述如何传递参数给@Test方法。
用于填充这个方法的参数的变量列表
@Test
标记一个类或方法作为测试的一部分
如果设置为true,这个测试方法将总是运行,甚至当它依赖的方法失败时。
dataProvider
这个测试方法的dataprovider的名称
dataProviderClass
用于查找dataprovider的类。
如果不指定,将在当前测试方法所在的类或者它的基类上查找dataprovider。
如果这个属性被指定,则dataprovider方法需要是指定类的static方法。
当前方法依赖的组列表
当前方法依赖的方法列表
description
当前方法的描述
当前类的方法/方法是否被激活
expectedExceptions
测试方法期望抛出的异常列表。
如果没有异常或者抛出的不是列表中的任何一个,当前方法都将标记为失败.
当前类/方法所属的组列表
invocationCount
当前方法被调用的次数
invocationTimeOut
该测试所被调用次数的最大耗时,单位毫秒。
如果调用次数未定义忽略本属性。
priority
测试方法优先级,低优先级的优先预订。
successPercentage
当前方法期望的成功率。
singleThreaded
如果设置为true,即使测试集现在运行方法设置为parallel="
methods"
,测试类中的所有方法运行在同一个线程中。
本属性只可以在类一级使用,忽略在方法一级的使用。
注:
依次调用本属性(已过时)
timeOut
当前方法容许花费的最大时间,单位毫秒。
threadPoolSize
当前方法的线程池大小。
方法将被多线程调用,次数由invocationCount参数指定
注意:
如果invocationCount没有指定则这个属性将被忽略
上面是TestNG中用到的annotation列表,从中我们可以看到TestNG提供的一些特性
1.before方法和after方法,带来了足够丰富的测试生命周期控制
2.dependsOnGroups/dependsOnMethods提供了依赖检查机制,并可以严格控制执行顺序
3.DataProvider使得对同一个方法的测试覆盖变的非常轻松,非常适合进行边界测试,只要给出多种测试数据就可以针对一个测试方法进行覆盖
4.expectedExceptions使得异常测试变的非常轻松
5.invocationCount/threadPoolSize终于可以简单的直接进行多线程测试了,这个绝对是junit的超级弱项,回想junit中那个万恶的System.exist(0)...
6.timeOut终于不用死等然后手工强行关闭测试,TestNG想的太周到了
testng.xml
调用TestNG由几种不同方法:
*使用testng.xml文件
*从命令行
这节描述testng.xml的格式。
当前testng.xml的DTD文件可以从官方找到:
http:
//testng.org/testng-1.0.dtd.php。
下面是testng.xml文件的一个例子:
<
!
DOCTYPEsuiteSYSTEM"
//testng.org/testng-1.0.dtd"
>
suitename="
Suite1"
verbose="
1"
testname="
Nopackage"
classes>
classname="
NoPackageTest"
/>
/classes>
/test>
Regression1"
test.sample.ParameterSample"
test.sample.ParameterTest"
/suite>
你可以指定包名替代类名:
packages>
packagename="
test.sample"
/packages>
在这个例子中,TestNG将在包test.sample中查找所有的类,并只保留带有TestNGannotation的类。
你同样可以指定包含或不包含的组和方法:
>
groups>
run>
excludename="
brokenTests"
includename="
checkinTests"
/run>
/groups>
test.IndividualMethodsTest"
methods>
testMethod"
/methods>
/class>
你同样可以在testng.xml中定义新的组,指定属性的额外详细情况,比如是否并行运行测试,使用多少线程,是否运行junit测试,等等...
请查看DTD文件了解完整的特性列表。
运行TestNG
TestNG可以以不同的方式调用:
*Commandline
*Eclipse
*IntelliJ'
sIDEA
1)命令行
假设你已经将TestNG加入到classpath,调用TestNG最简单的方法事下面的:
javaorg.testng.TestNGtestng1.xml[testng2.xmltestng3.xml...]
必须指定最少一个描述你试图测试的TestNGsuite的xml文件。
另外,下面的命令行参数可以使用:
命令行参数列表
操作
参数
说明
-configfailurepolicy
skip|continue
TestNG是否继续执行suite中剩下的测试集,或者跳过@Before*失败方法。
模式习惯跳过。
-d
Adirectory
Thedirectorywherethereportswillbegenerated(defaultsto
test-output).
-dataproviderthreadcount
Thedefaultnumberofthreadstousefordataproviderswhenrunningtestsinparallel.
Thissetsthedefaultmaximumnumberofthreadstousefordataproviderswhenrunningtestsinparallel.Itwillonlytakeeffectiftheparallelmodehasbeenselected(forexample,withthe-paralleloption).Thiscanbeoverriddeninthesuitedefinition.
-excludegroups
Acomma-separatedlistofgroups.
Thelistofgroupsyouwanttobeexcludedfromthisrun.
-groups
Thelistofgroupsyouwanttorun(e.g.
"
windows,linux,regression"
).
-listener
Acomma-separatedlistofJavaclassesthatcanbefoundonyourclasspath.
Letsyouspecifyyourowntestlisteners.Theclassesneedtoimplement
org.testng.ITestListener
-methods
Acommaseparatedlistoffullyqualifiedclassnameandmethod.Forexamplecom.example.Foo.f1,com.example.Bar.f2.
Letsyouspecifyindividualmethodstorun.
-methodselectors
Acomma-separatedlistofJavaclassesandmethodprioritiesthatdefinemethodselectors.
Letsyouspecifymethodselectorsonthecommandline.Forexample:
com.example.Selector1:
3,com.example.Selector2:
2
-parallel
methods|tests|classes
Ifspecified,setsthedefaultmechanismusedtodeterminehowtouseparallelthreadswhenrunningtests.Ifnotset,defaultmechanismisnottouseparallelthreadsatall.Thiscanbeoverriddeninthesuitedefinition.
-reporter
Theextendedconfigurationforacustomreportlistener.
Similartothe
-listener
option,exceptthatitallowstheconfigurationofJavaBeans-stylepropertiesonthereporterinstance.
Example:
-reportercom.test.MyReporter:
methodFilter=*insert*,enableFiltering=true
Youcanhaveasmanyoccurencesofthisoption,oneforeachreporterthatneedstobeadded.
-sourcedir
Asemi-colonseparatedlistofdirectories.
Thedirectorieswhereyourjavadocannotatedtestsourcesare.Thisoptionisonlynecessaryifyouareusingjavadoctypeannotations.(e.g.
src/test"
or
src/test/org/testng/eclipse-plugin;
src/test/org/testng/testng"
-suitename
Thedefaultnametouseforatestsuite.
Thisspecifiesthesuitenameforatestsuitedefinedonthecommandline.Thisoptionisignoredifthesuite.xmlfileorthesourcecodespecifiesadifferentsuitename.Itispossibletocreateasuitenamewithspacesinitifyousurrounditwithdouble-quotes"
likethis"
.
-testclass
Acomma-separatedlistofclassesthatcanbefoundinyourclasspath.
Alistofclassfilesseparatedbycommas(e.g.
org.foo.Test1,org.foo.test2"
-testjar
Ajarfile.
Specifiesajarfilethatcontainstestclasses.Ifa
testng.xml
fileisfoundattherootofthatjarfile,itwillbeused,otherwise,allthetestclassesfoundinthisjarfilewillbeconsideredtestclasses.
-testname
Thedefaultnametouseforatest.
Thisspecifiesthenameforatestdefinedonthecommandline.Thisoptionisignoredifthesuite.xmlfileorthesourcecodespecifiesadifferenttestname.Itispossibletocreateatestnamewithspacesinitifyousurrounditwithdouble-quotes"
-testnames
Acommaseparatedlistoftestnames.
Onlytestsdefinedina<
tagmatchingoneofthesenameswillberun.
-testrunfactory
AJavaclassesthatcanbefoundonyourclasspath.
Letsyouspecifyyourowntestrunners.Theclassneedstoimplement
org.testng.ITestRunnerFactory.
-threadcount
Thedefaultnumberofthreadstousewhenrunningtestsinparallel.
Thissetsthedefaultmaximumnumberofthreadstouseforrunningtestsinparallel.Itwillonlytakeeffectiftheparallelmodehasbeenselected(forexample,withthe-paralleloption).Thiscanbeoverriddeninthesuitedefinition.
-xmlpathinjar
ThepathoftheXMLfileinsidethejarfile.
ThisattributeshouldcontainthepathtoavalidXMLfileinsidethetestjar(e.g.
resources/testng.xml"
).Thedefaultis
testng.xml"
whichmeansafilecalled"
attherootofthejarfile.Thisoptionwillbeignoredunless
-testjar
isspecif
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- TestNG 使用说明