TestNg(The Next Generation testing)--3

来源:互联网 发布:淘宝客招募贴怎么写 编辑:程序博客网 时间:2024/05/29 18:03

3、Testng.xml

你可以使用下面几种方法调用TestNg框架

3.1使用Testng.xml

3.2使用Ant

3.3使用命令行

这个小节,将会住要描述一下如何使用testng.xml,以及testng.xml的格式。testng.xml使用的是testng.dtd进行的约束,如果您对dtd标记语言非常熟悉,直接看这个文件即可读懂其中的意思和格式,dtd的描述如下所示:

<!--Here is a quick overview of the main parts of this DTD.  For more information,refer to the <a href="http://testng.org">main web site</a>.                                                      A <b>suite</b> is made of <b>tests</b> and <b>parameters</b>.                                                      A <b>test</b> is made of three parts:                        <ul><li> <b>parameters</b>, which override the suite parameters     <li> <b>groups</b>, made of two parts                           <li> <b>classes</b>, defining which classes are going to be part  of this test run                                    </ul>                                                      In turn, <b>groups</b> are made of two parts:                <ul><li> Definitions, which allow you to group groups into     bigger groups                                       <li> Runs, which defines the groups that the methods       must belong to in order to be run during this test  </ul>                                                      Cedric Beust & Alexandru Popescu                      @title DTD for TestNG                                    @root suite--><!-- A suite is the top-level element of a testng.xml file                  --><!ELEMENT suite (groups?,(listeners|packages|test|parameter|method-selectors|suite-files)*) ><!-- Attributes: --><!--@attr  name        The name of this suite (as it will appear in the reports)@attr  junit       Whether to run in JUnit mode.@attr  verbose     How verbose the output on the console will be.                  This setting has no impact on the HTML reports.@attr  parallel   Whether TestNG should use different threads                to run your tests (might speed up the process)@attr  configfailurepolicy  Whether to continue attempting Before/After                Class/Methods after they've failed once or just skip remaining.@attr  thread-count An integer giving the size of the thread pool to use                if you set parallel.@attr  annotations  If "javadoc", TestNG will look for                JavaDoc annotations in your sources, otherwise it will                use JDK5 annotations.@attr  time-out     The time to wait in milliseconds before aborting the                method (if parallel="methods") or the test (parallel="tests")@attr  skipfailedinvocationcounts Whether to skip failed invocations.@attr  data-provider-thread-count An integer givin the size of the thread pool to use       for parallel data providers.@attr  object-factory A class that implements IObjectFactory that will be used to       instantiate the test objects.@attr allow-return-values If true, tests that return a value will be run as well--><!ATTLIST suite     name CDATA #REQUIRED    junit (true | false) "false"    verbose CDATA #IMPLIED    parallel (false | methods | tests | classes | instances) "false"    configfailurepolicy (skip | continue) "skip"    thread-count CDATA "5"    annotations CDATA #IMPLIED    time-out CDATA #IMPLIED    skipfailedinvocationcounts (true | false) "false"    data-provider-thread-count CDATA "10"    object-factory CDATA #IMPLIED    group-by-instances (true | false) "false"    preserve-order (true | false) "true"    allow-return-values (true | false) "false"><!-- A list of XML files that contain more suite descriptions --><!ELEMENT suite-files (suite-file)* ><!ELEMENT suite-file ANY ><!ATTLIST suite-file    path CDATA #REQUIRED><!--Parameters can be defined at the <suite> or at the <test> level.Parameters defined at the <test> level override parameters of the same name in <suite>Parameters are used to link Java method parameters to their actual value, defined here.--><!ELEMENT parameter ANY><!ATTLIST parameter    name CDATA #REQUIRED    value CDATA #REQUIRED ><!--Method selectors define user classes used to select which methods to run.They need to implement <tt>org.testng.IMethodSelector</tt> --><!ELEMENT method-selectors (method-selector*) ><!ELEMENT method-selector ((selector-class)*|script) ><!ELEMENT selector-class ANY><!ATTLIST selector-class    name CDATA #REQUIRED  priority CDATA #IMPLIED><!ELEMENT script ANY><!ATTLIST script    language CDATA #REQUIRED><!--A test contains parameters and classes.  Additionally, you can define additional groups ("groups of groups")--><!ELEMENT test (method-selectors?,parameter*,groups?,packages?,classes?) ><!--@attr  name         The name of this test (as it will appear in the reports)@attr  junit        Whether to run in JUnit mode.@attr  verbose      How verbose the output on the console will be.                This setting has no impact on the HTML reports.                Default value: suite level verbose.@attr  parallel     Whether TestNG should use different threads                to run your tests (might speed up the process)@attr  thread-count An integer giving the size of the thread pool to be used if                parallel mode is used. Overrides the suite level value.@attr  annotations  If "javadoc", TestNG will look for                JavaDoc annotations in your sources, otherwise it will                use JDK5 annotations.@attr  time-out     the time to wait in milliseconds before aborting                the method (if parallel="methods") or the test (if parallel="tests")@attr  enabled      flag to enable/disable current test. Default value: true @attr  skipfailedinvocationcounts Whether to skip failed invocations.@attr preserve-order If true, the classes in this tag will be run in the same order asfound in the XML file.@attr allow-return-values If true, tests that return a value will be run as well--><!ATTLIST test    name CDATA #REQUIRED     junit (true | false) "false"    verbose  CDATA #IMPLIED    parallel  CDATA #IMPLIED    thread-count CDATA #IMPLIED    annotations  CDATA #IMPLIED    time-out CDATA #IMPLIED    enabled CDATA #IMPLIED    skipfailedinvocationcounts (true | false) "false"    preserve-order (true | false) "true"    group-by-instances (true | false) "false"    allow-return-values (true | false) "false"><!--Defines additional groups ("groups of groups") and also which groups to include in this test run--><!ELEMENT groups (define*,run?,dependencies?) ><!ELEMENT define (include*)><!ATTLIST define    name CDATA #REQUIRED><!-- Defines which groups to include in the current group of groups         --><!ELEMENT include ANY><!ATTLIST include    name CDATA #REQUIRED    description CDATA #IMPLIED    invocation-numbers CDATA #IMPLIED><!-- Defines which groups to exclude from the current group of groups       --><!ELEMENT exclude ANY><!ATTLIST exclude    name CDATA #REQUIRED><!-- The subtag of groups used to define which groups should be run         --><!ELEMENT run (include?,exclude?)* ><!ELEMENT dependencies (group*)><!ELEMENT group ANY><!ATTLIST group    name CDATA #REQUIRED    depends-on CDATA #REQUIRED><!-- The list of classes to include in this test                            --><!ELEMENT classes (class*,parameter*) ><!ELEMENT class (methods*) ><!ATTLIST class    name CDATA #REQUIRED ><!-- The list of packages to include in this test                           --><!ELEMENT packages (package*) ><!-- The package description.      If the package name ends with .* then subpackages are included too.--><!ELEMENT package (include?,exclude?)*><!ATTLIST package    name CDATA #REQUIRED ><!-- The list of methods to include/exclude from this test                 --><!ELEMENT methods (include?,exclude?,parameter?)* ><!-- The list of listeners that will be passed to TestNG --><!ELEMENT listeners (listener*) ><!ELEMENT listener ANY><!ATTLIST listener    class-name CDATA #REQUIRED >
其中
<!ELEMENT suite (groups?,(listeners|packages|test|parameter|method-selectors|suite-files)*) >
表示,文件必须以suite开始,并且有0~1个groups子元素,若干个(listeners|packages|test|parameter|method-selectors|suite-files)元素。

我们来看看如下的几个实例:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >  <suite name="Suite1" verbose="1" >  <test name="Nopackage" >    <classes>       <class name="NoPackageTest" />    </classes>  </test>   <test name="Regression1">    <classes>      <class name="test.sample.ParameterSample"/>      <class name="test.sample.ParameterTest"/>    </classes>  </test></suite>

上述配置文件中,表示需要运行类NoPackageTest,test.sample.ParameterSample,

test.sample.ParameterTest中所有的testcase。

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" >  <test name="Regression1"   >    <packages>      <package name="test.sample" />   </packages> </test></suite>
采用包的配置形式,在test.sample包下的所有test都会被执行。

<test name="Regression1">  <groups>    <run>      <exclude name="brokenTests"  />      <include name="checkinTests"  />    </run>  </groups>    <classes>    <class name="test.IndividualMethodsTest">      <methods>        <include name="testMethod" />      </methods>    </class>  </classes></test>


上述配置表示,brokenTests分组将会排除此次运行,但是checkinTests将会被运行,并且在class 

test.IndividualMethodsTest中的testMethod将会被运行。


在默认情况下,TestNg运行您的测试用例会根据xml的配置有一个默认的顺序,如果您想要随机的方式运行测试用

例,则需要设置属性 preserve-order在suite上,如下所示。

<test name="Regression1" preserve-order="false">  <classes>     <class name="test.Test1">      <methods>        <include name="m1" />        <include name="m2" />      </methods>    </class>     <class name="test.Test2" />   </classes></test>






1 0
原创粉丝点击