首页 > 技术文章 > TestNG BeforeClass BeforeMethod Test AfterClass AfterMethod

robbinluobo 2016-11-30 16:09 原文

http://topmanopensource.iteye.com/blog/1983729

1.TestNG测试注解和Junit注解的不同以及生命周期:

TestNG测试的一个方法的生命周期:

                @BeforeClass(执行一次)

                @BeforeMethod(N个Test 方法执行N次)

                @Test Test方法(此注解可能在类上表示多个,在方法表示一个)

                @AfterMethod(N个Test 方法执行N次)

                @AfterClass(执行一次)

Junit4测试的一个方法的生命周期:

               @BeforeClass(执行一次)

              @Before(N个Test 方法执行N次)

               @Test Test方法

              @After(N个Test 方法执行N次)

              @AfterClass(执行一次)

2.测试@Test的使用范围和分组

下面为源代码自己看不解释:

Java代码  收藏代码
  1. package org.testng.annotations;  
  2.   
  3. import static java.lang.annotation.ElementType.CONSTRUCTOR;  
  4. import static java.lang.annotation.ElementType.METHOD;  
  5. import static java.lang.annotation.ElementType.TYPE;  
  6.   
  7. import java.lang.annotation.Retention;  
  8. import java.lang.annotation.Target;  
  9.   
  10. /** 
  11.  * Mark a class or a method as part of the test. 
  12.  * 
  13.  * @author Cedric Beust, Apr 26, 2004 
  14.  */  
  15. @Retention(java.lang.annotation.RetentionPolicy.RUNTIME)  
  16. @Target({METHOD, TYPE, CONSTRUCTOR})  
  17. public @interface Test {  
  18.   /** 
  19.    * The list of groups this class/method belongs to. 
  20.    */  
  21.   public String[] groups() default {};  
  22.   
  23.   /** 
  24.    * Whether methods on this class/method are enabled. 
  25.    */  
  26.   public boolean enabled() default true;  
  27.   
  28.   /** 
  29.    * The list of variables used to fill the parameters of this method. 
  30.    * These variables must be defined in the property file. 
  31.    * 
  32.    * @deprecated Use @Parameters 
  33.    */  
  34.   @Deprecated  
  35.   public String[] parameters() default {};  
  36.   
  37.   /** 
  38.    * The list of groups this method depends on.  Every method 
  39.    * member of one of these groups is guaranteed to have been 
  40.    * invoked before this method.  Furthermore, if any of these 
  41.    * methods was not a SUCCESS, this test method will not be 
  42.    * run and will be flagged as a SKIP. 
  43.    */  
  44.   public String[] dependsOnGroups() default {};  
  45.   
  46.   /** 
  47.    * The list of methods this method depends on.  There is no guarantee 
  48.    * on the order on which the methods depended upon will be run, but you 
  49.    * are guaranteed that all these methods will be run before the test method 
  50.    * that contains this annotation is run.  Furthermore, if any of these 
  51.    * methods was not a SUCCESS, this test method will not be 
  52.    * run and will be flagged as a SKIP. 
  53.    * 
  54.    * If some of these methods have been overloaded, all the overloaded 
  55.    * versions will be run. 
  56.    */  
  57.   public String[] dependsOnMethods() default {};  
  58.   
  59.   /** 
  60.    * The maximum number of milliseconds this test should take. 
  61.    * If it hasn't returned after this time, it will be marked as a FAIL. 
  62.    */  
  63.   public long timeOut() default 0;  
  64.   
  65.   /** 
  66.    * The maximum number of milliseconds that the total number of invocations on this test 
  67.    * method should take.  This annotation will be ignored if the attribute invocationCount 
  68.    * is not specified on this method. 
  69.    * If it hasn't returned after this time, it will be marked as a FAIL. 
  70.    */  
  71.   public long invocationTimeOut() default 0;  
  72.   
  73.   /** 
  74.    * The number of times this method should be invoked. 
  75.    */  
  76.   public int invocationCount() default 1;  
  77.   
  78.   /** 
  79.    * The size of the thread pool for this method.  The method will be invoked 
  80.    * from multiple threads as specified by invocationCount. 
  81.    * Note:  this attribute is ignored if invocationCount is not specified 
  82.    */  
  83.   public int threadPoolSize() default 0;  
  84.   
  85.   /** 
  86.    * The percentage of success expected from this method. 
  87.    */  
  88.   public int successPercentage() default 100;  
  89.   
  90.   /** 
  91.    * The name of the data provider for this test method. 
  92.    * @see org.testng.annotations.DataProvider 
  93.    */  
  94.   public String dataProvider() default "";  
  95.   
  96.   /** 
  97.    * The class where to look for the data provider.  If not 
  98.    * specified, the dataprovider will be looked on the class 
  99.    * of the current test method or one of its super classes. 
  100.    * If this attribute is specified, the data provider method 
  101.    * needs to be static on the specified class. 
  102.    */  
  103.   public Class<?> dataProviderClass() default Object.class;  
  104.   
  105.   /** 
  106.    * If set to true, this test method will always be run even if it depends 
  107.    * on a method that failed.  This attribute will be ignored if this test 
  108.    * doesn't depend on any method or group. 
  109.    */  
  110.   public boolean alwaysRun() default false;  
  111.   
  112.   /** 
  113.    * The description for this method.  The string used will appear in the 
  114.    * HTML report and also on standard output if verbose >= 2. 
  115.    */  
  116.   public String description() default "";  
  117.   
  118.   /** 
  119.    * The list of exceptions that a test method is expected to throw.  If no 
  120.    * exception or a different than one on this list is thrown, this test will be 
  121.    * marked a failure. 
  122.    */  
  123.   public Class[] expectedExceptions() default {};  
  124.   
  125.   /** 
  126.    * If expectedExceptions was specified, its message must match the regular expression 
  127.    * specified in this attribute. 
  128.    */  
  129.   public String expectedExceptionsMessageRegExp() default ".*";  
  130.   
  131.   /** 
  132.    * The name of the suite this test class should be placed in.  This 
  133.    * attribute is ignore if @Test is not at the class level. 
  134.    */  
  135.   public String suiteName() default "";  
  136.   
  137.   /** 
  138.    * The name of the test  this test class should be placed in.  This 
  139.    * attribute is ignore if @Test is not at the class level. 
  140.    */  
  141.   public String testName() default "";  
  142.   
  143.   /** 
  144.    * @deprecated Use singleThreaded 
  145.    */  
  146.   public boolean sequential() default false;  
  147.   
  148.   /** 
  149.    * If set to true, all the methods on this test class are guaranteed to run 
  150.    * in the same thread, even if the tests are currently being run with parallel="true". 
  151.    * 
  152.    * This attribute can only be used at the class level and will be ignored 
  153.    * if used at the method level. 
  154.    */  
  155.   public boolean singleThreaded() default false;  
  156.   
  157.   /** 
  158.    * The name of the class that should be called to test if the test 
  159.    * should be retried. 
  160.    * @return String The name of the class that will test if a test method 
  161.    * should be retried. 
  162.    */  
  163.   public Class retryAnalyzer() default Class.class;  
  164.   
  165.   /** 
  166.    * If true and invocationCount is specified with a value > 1, 
  167.    * then all invocations after a failure will be marked as a SKIP 
  168.    * instead of a FAIL. 
  169.    */  
  170.   public boolean skipFailedInvocations() default false;  
  171.   
  172.   /** 
  173.    * If set to true, this test will run even if the methods 
  174.    * it depends on are missing or excluded. 
  175.    */  
  176.   public boolean ignoreMissingDependencies() default false;  
  177.   
  178.   /** 
  179.    * The scheduling priority. Lower priorities will be scheduled first. 
  180.    */  
  181.   int priority() default 0;  
  182.   
  183. }  

 

3.TestNG参数化测试

        A.基于xml配置实现 需要@Parameters配合

        B.基于编码方式需要@Parameters配合

        C.基于Dataprovider数据提供者实现的。

        D.基于自定义数据提供者。

4.TestNG测试之间依赖

        A.测试分组,组间依赖   dependsOnGroups

        B.测试方法,方法名称之间的依赖   dependsOnMethods

5. TestNG @Factory测试工厂的设置

       主要设置针对测试单元的多次测试而言

6.TestNG并发测试和超时,异常的设置 threadPoolSize  singleThreaded         timeOut  expectedExceptions

 

7TestNG失败重新执行的策略

8TestNG和junit的整合

     A.junit3 采用反射实现调用和执行

    B.junit4采用JunitCore调用执行类。

9TestNG编码方式运行

       TestNG实现:

         

Java代码  收藏代码
  1. TestNG tng = new TestNG();    
  2.     tng.addListener( new MyMethodInterceptor());  
  3.     tng.setTestClasses(new Class[]{MyMethodInterceptorTest.class});  
  4.     tng.run();  

 

 

Junit4实现:

  

Java代码  收藏代码
  1. JUnitCore.main(args);  
  2. tCore.runClasses(new class[]{Test.class,Test1.class});  

 

10。TestNG通过注解Transformers 实现对运行时执行控制。

               主要实现IAnnotationTransformer 接口即可。

11.TestNg方法拦截器的可以修改执行的结果信息。

              主要实现IMethodInterceptor接口即可。

12 TestNG丰富的Listener监听器满足不同阶段的监听。

  IAnnotationTransformer  
  IAnnotationTransformer2  
  IHookable  
  IInvokedMethodListener  
  IMethodInterceptor  
  IReporter  
  ISuiteListener  
  ITestListener 

13.TestNG的依赖注入和不同框架的整合

    Spring,Guide等使用。

14.TestNG结果的监听和执行报告和日志的控制

推荐阅读