`
longgangbai
  • 浏览: 7250512 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

TestNG简单的学习(十一)TestNG学习总结

阅读更多

       最近一直在学习关于TestNG方面的知识,根据最近的学习总结以下几点:

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的使用范围和分组

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

package org.testng.annotations;

import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
 * Mark a class or a method as part of the test.
 *
 * @author Cedric Beust, Apr 26, 2004
 */
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD, TYPE, CONSTRUCTOR})
public @interface Test {
  /**
   * The list of groups this class/method belongs to.
   */
  public String[] groups() default {};

  /**
   * Whether methods on this class/method are enabled.
   */
  public boolean enabled() default true;

  /**
   * The list of variables used to fill the parameters of this method.
   * These variables must be defined in the property file.
   *
   * @deprecated Use @Parameters
   */
  @Deprecated
  public String[] parameters() default {};

  /**
   * The list of groups this method depends on.  Every method
   * member of one of these groups is guaranteed to have been
   * invoked before this method.  Furthermore, if any of these
   * methods was not a SUCCESS, this test method will not be
   * run and will be flagged as a SKIP.
   */
  public String[] dependsOnGroups() default {};

  /**
   * The list of methods this method depends on.  There is no guarantee
   * on the order on which the methods depended upon will be run, but you
   * are guaranteed that all these methods will be run before the test method
   * that contains this annotation is run.  Furthermore, if any of these
   * methods was not a SUCCESS, this test method will not be
   * run and will be flagged as a SKIP.
   *
   * If some of these methods have been overloaded, all the overloaded
   * versions will be run.
   */
  public String[] dependsOnMethods() default {};

  /**
   * The maximum number of milliseconds this test should take.
   * If it hasn't returned after this time, it will be marked as a FAIL.
   */
  public long timeOut() default 0;

  /**
   * The maximum number of milliseconds that the total number of invocations on this test
   * method should take.  This annotation will be ignored if the attribute invocationCount
   * is not specified on this method.
   * If it hasn't returned after this time, it will be marked as a FAIL.
   */
  public long invocationTimeOut() default 0;

  /**
   * The number of times this method should be invoked.
   */
  public int invocationCount() default 1;

  /**
   * The size of the thread pool for this method.  The method will be invoked
   * from multiple threads as specified by invocationCount.
   * Note:  this attribute is ignored if invocationCount is not specified
   */
  public int threadPoolSize() default 0;

  /**
   * The percentage of success expected from this method.
   */
  public int successPercentage() default 100;

  /**
   * The name of the data provider for this test method.
   * @see org.testng.annotations.DataProvider
   */
  public String dataProvider() default "";

  /**
   * The class where to look for the data provider.  If not
   * specified, the dataprovider will be looked on the class
   * of the current test method or one of its super classes.
   * If this attribute is specified, the data provider method
   * needs to be static on the specified class.
   */
  public Class<?> dataProviderClass() default Object.class;

  /**
   * If set to true, this test method will always be run even if it depends
   * on a method that failed.  This attribute will be ignored if this test
   * doesn't depend on any method or group.
   */
  public boolean alwaysRun() default false;

  /**
   * The description for this method.  The string used will appear in the
   * HTML report and also on standard output if verbose >= 2.
   */
  public String description() default "";

  /**
   * The list of exceptions that a test method is expected to throw.  If no
   * exception or a different than one on this list is thrown, this test will be
   * marked a failure.
   */
  public Class[] expectedExceptions() default {};

  /**
   * If expectedExceptions was specified, its message must match the regular expression
   * specified in this attribute.
   */
  public String expectedExceptionsMessageRegExp() default ".*";

  /**
   * The name of the suite this test class should be placed in.  This
   * attribute is ignore if @Test is not at the class level.
   */
  public String suiteName() default "";

  /**
   * The name of the test  this test class should be placed in.  This
   * attribute is ignore if @Test is not at the class level.
   */
  public String testName() default "";

  /**
   * @deprecated Use singleThreaded
   */
  public boolean sequential() default false;

  /**
   * If set to true, all the methods on this test class are guaranteed to run
   * in the same thread, even if the tests are currently being run with parallel="true".
   *
   * This attribute can only be used at the class level and will be ignored
   * if used at the method level.
   */
  public boolean singleThreaded() default false;

  /**
   * The name of the class that should be called to test if the test
   * should be retried.
   * @return String The name of the class that will test if a test method
   * should be retried.
   */
  public Class retryAnalyzer() default Class.class;

  /**
   * If true and invocationCount is specified with a value > 1,
   * then all invocations after a failure will be marked as a SKIP
   * instead of a FAIL.
   */
  public boolean skipFailedInvocations() default false;

  /**
   * If set to true, this test will run even if the methods
   * it depends on are missing or excluded.
   */
  public boolean ignoreMissingDependencies() default false;

  /**
   * The scheduling priority. Lower priorities will be scheduled first.
   */
  int priority() default 0;

}

 

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实现:

         

	TestNG tng = new TestNG();  
		tng.addListener( new MyMethodInterceptor());
		tng.setTestClasses(new Class[]{MyMethodInterceptorTest.class});
		tng.run();

 

 

Junit4实现:

  

	     JUnitCore.main(args);
		JUnitCore.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结果的监听和执行报告和日志的控制

 

 

 

   

分享到:
评论

相关推荐

    TestNG中文手册学习笔记

    TestNG中文手册及学习笔记,包括:环境配置、详细使用说明及实例。

    testNG单元测试学习总结

    配合先前的代码demo,进行了一个系统的学习总结

    TestNG学习笔记

    学习笔记资料来源:http://www.yiibai.com/testng/;从易佰TestNG教程整理,包括概念介绍,代码实例。

    TestNG学习中遇到的坑.txt

    2017年6月5日记:在学习直接用手工执行testng.xml时,得到以下教训: 1、如果写批处理调用,环境变量的末尾必须以分号结束,否则调试气死你也搞不定啥原因,控制台就一句话,找不到类;

    TestNG安装包

    TestNG是一个Java的框架,所以第一个要求是JDK要安装在你的机器上。 系统要求 JDK 1.5或以上 内存 没有最低要求 磁盘空间 没有最低要求 操作系统 没有最低要求 步骤1 -验证Java安装在你的机器上 现在,打开...

    testNG6.11.0

    1.下载testNG 离线安装包【eclipse-testng离线包】,并解压。 2.将解压后的文件..\eclipse-testng离线包\features\目录下的文件夹org.testng.eclipse_6.11.0.201703011520放到eclipse安装路径下的features目录下 3....

    testng测试报告模板BeautifulReport.7z

    1、testng测试报告模板BeautifulReport 2、testng.xml文件

    TestNG的离线安装包

    1. 将解压后的文件..\eclipse-testng离线包\features\org.testng.eclipse_6.9.9.201510270734 文件夹 放到 eclipse--》features目录下 2. 将解压后的文件..\eclipse-testng离线包\plugins\org.testng.eclipse_6.9...

    testng-6.3.jar

    testNG 自动化测试工具 工具类 testng-6.3.jar

    testng-6.9.4和testng-6.8.8.zip

    testng-6.8.8

    testng-6.9-10

    testng-6.9-10 jar包

    eclipse 安装testNG 离线包

    1. 将解压后的文件..\eclipse-testng离线包\features\org.testng.eclipse_6.9.9.201510270734 文件夹 放到 eclipse--》features目录下 2. 将解压后的文件..\eclipse-testng离线包\plugins\org.testng.eclipse_...

    testNg介绍

    testNg介绍

    testNG-6.14.jar

    testNG-6.14.jar,最新版的testng测试框架分享给大家,共同学习……

    [TestNG] TestNG 初学者指南 (英文版)

    [Packt Publishing] TestNG 初学者指南 (英文版) [Packt Publishing] TestNG Beginner's Guide (E-Book) ☆ 图书概要:☆ Write robust unit and functional tests with the power of TestNG Overview Step-by-...

    testng-6.9.6.jar

    最新testng jar包,testng-6.9.6.jar

    testNG eclipse插件包

    2.将解压后的文件..\eclipse-testng离线包\features\目录下的文件夹org.testng.eclipse_6.8.6.20130607_0745放到eclipse--&gt;features目录下 3.将解压后的文件..\eclipse-testng离线包\org.testng.eclipse_6.8.6....

    eclipse-testng离线安装包

    1.下载testNG 离线安装包【eclipse-testng离线包】,并解压。 2.将解压后的文件..\eclipse-testng离线包\features\目录下的文件夹org.testng.eclipse_6.9.8.201510130443放到eclipse安装路径下的features目录下 3.将...

    testng-xslt-1.1.2.zip

    testng-xslt-1.1.2.zip

    TestNG学习文档英文版.pdf版

    TestNG学习文档英文版,如果你想了解TestNG细则,以及各种语法,值得下载看看

Global site tag (gtag.js) - Google Analytics