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

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

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

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

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

Junit4实现:

  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结果的监听和执行报告和日志的控制

TestNG BeforeClass BeforeMethod Test AfterClass AfterMethod的更多相关文章

  1. Mockito @BeforeClass @BeforeMethod @BeforeTest 的生命周期

    @BeforeClass---@AfterClass 类实例化前, 被执行, 主要用于设置环境变量等, 与SpringTestContext结合用的时候要注意, 这种情况下@autowire的bean ...

  2. testng生成自定义html报告

    转自:https://blog.csdn.net/kdslkd/article/details/51198433 testng原生的或reportng的报告总有些不符合需要,尝试生成自定义测试报告,用 ...

  3. 测试报告 之 testNG + Velocity 编写自定义html测试报告

    之前用testNG自带的test-outputemailable-report.html,做出的UI自动化测试报告,页面不太好看. 在网上找到一个新的报告编写,自己尝试了一下,埋了一些坑,修改了输出时 ...

  4. testng自定义html报告,根据freemaker生成

    [转] https://testerhome.com/topics/3487 [参考]https://www.cnblogs.com/cheese320/p/8890929.html  做了些修改,换 ...

  5. APP接口自动化测试JAVA+TestNG(二)之TestNG简介与基础实例

    前言 继上篇环境篇后,本篇主要对TestNG进行介绍,给出最最基础的两个实例,通过本文后,学会并掌握TestNG测试用例的编写与运行,以及生成美化后的报告.下一篇为HTTP接口实战(国家气象局接口自动 ...

  6. testng 教程

    Testng 简介: Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性: annotations  注释,如 @test ...

  7. TestNG之注解的生命周期

    有必要介绍一下TestNG注解的生命周期,先看一下官网支持的注解有 @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeGroups@AfterGro ...

  8. Testng使用方法示例

    TestNG TestNG是一个测试框架,灵感来自JUnit和NUnit.但引入了下面这些新的功能,使它更强大和更容易使用. 注解: 可在任意大的线程池运行您的测试(所有方法在它们自己的线程内,一个线 ...

  9. TestNG学习-002-annotaton 注解概述及其执行顺序

    此文主要讲述用 TestNG 基础的 annotation (注解)知识,及其执行的顺序,并通过一个 TestNG 简单的实例演示 annotation 的执行顺序. 希望能对初学 TestNG 测试 ...

随机推荐

  1. 用自己赚的钱第一次坐飞机 那feel倍儿爽

    马年春晚上,歌手大张伟的神曲<倍儿爽>如今已传遍大街小巷.其实,不管人家到底有没有炒作,能让我们这些观众感觉到放松.乐呵,那就是一个成功的春晚节目.而今年,我也如同这歌中唱的一样,活得倍儿 ...

  2. Excel应该这么玩——5、三种数据:Excel也是系统

        Excel最常用的功能就是记录数据,把数据按照行列记录下来.这部分数据是源数据,是业务活动中最原始的流水账,作为后续操作的依据.为了从源数据中得出一定的结论,需要对源数据进行分析得出报表数据. ...

  3. jquery 通过ajax FormData 对象上传附件

    之前上传附件都是用插件,或者用form表单体检(这个是很久以前的方式了),今天突发奇想,自己来实现附件上传,具体实现如下 html: <div>   流程图: <input id=& ...

  4. EF并非我们想象的那么智能

    我之前在项目中用EF读取一个视图的数据,页面展示出现重复数据,当时百思不得其解,跟踪代码,数据读取时取到的数据并不是重复,为什么在前台显示就有重复了呢,我当时就在业务层将数据去重,但取到的数据跟数据库 ...

  5. 今天开始Swift学习

    今天开始Swift学习  在此记录笔记  以备之后查阅! allenhuang

  6. SOA和WCF&WebAPI

    SOA http://www.cnblogs.com/leslies2/archive/2011/12/12/2272722.html WCF开发框架形成之旅--如何实现X509证书加密 WebAPI ...

  7. redis.conf配置详细翻译解析

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...

  8. webpack react基础配置二 热加载

    用到 webpack-dev-server  先安装,注意 装到全局 还是本项目我也没注意  因为之前一直报错,有很小可能是安装到本地解决了,或者是我网络问题: 装到全局:$ npm install ...

  9. JAVA 中XML的解析

    XML:  可扩展标记语言(extensible Markup Language) 用于标记电子文件使其具有结构性的标记语言.XML可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定 ...

  10. 修改bootstrap modal模态框的宽度

    原文链接:http://blog.csdn.net/wuhawang/article/details/52252912 修改模态框的宽度很简单,修改width属性就可以了 但是要注意的一点是,修改的不 ...