TestNG BeforeClass BeforeMethod Test AfterClass AfterMethod
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的使用范围和分组
下面为源代码自己看不解释:
- 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);
- 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的更多相关文章
- Mockito @BeforeClass @BeforeMethod @BeforeTest 的生命周期
@BeforeClass---@AfterClass 类实例化前, 被执行, 主要用于设置环境变量等, 与SpringTestContext结合用的时候要注意, 这种情况下@autowire的bean ...
- testng生成自定义html报告
转自:https://blog.csdn.net/kdslkd/article/details/51198433 testng原生的或reportng的报告总有些不符合需要,尝试生成自定义测试报告,用 ...
- 测试报告 之 testNG + Velocity 编写自定义html测试报告
之前用testNG自带的test-outputemailable-report.html,做出的UI自动化测试报告,页面不太好看. 在网上找到一个新的报告编写,自己尝试了一下,埋了一些坑,修改了输出时 ...
- testng自定义html报告,根据freemaker生成
[转] https://testerhome.com/topics/3487 [参考]https://www.cnblogs.com/cheese320/p/8890929.html 做了些修改,换 ...
- APP接口自动化测试JAVA+TestNG(二)之TestNG简介与基础实例
前言 继上篇环境篇后,本篇主要对TestNG进行介绍,给出最最基础的两个实例,通过本文后,学会并掌握TestNG测试用例的编写与运行,以及生成美化后的报告.下一篇为HTTP接口实战(国家气象局接口自动 ...
- testng 教程
Testng 简介: Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性: annotations 注释,如 @test ...
- TestNG之注解的生命周期
有必要介绍一下TestNG注解的生命周期,先看一下官网支持的注解有 @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeGroups@AfterGro ...
- Testng使用方法示例
TestNG TestNG是一个测试框架,灵感来自JUnit和NUnit.但引入了下面这些新的功能,使它更强大和更容易使用. 注解: 可在任意大的线程池运行您的测试(所有方法在它们自己的线程内,一个线 ...
- TestNG学习-002-annotaton 注解概述及其执行顺序
此文主要讲述用 TestNG 基础的 annotation (注解)知识,及其执行的顺序,并通过一个 TestNG 简单的实例演示 annotation 的执行顺序. 希望能对初学 TestNG 测试 ...
随机推荐
- 夺命雷公狗-----React---20--实现验证码大写
<!DOCTYPE> <html> <head> <meta charset="utf-8"> <title></ ...
- failed to lazily initialize a collection of role:XXX, no sessi
系统 框架 springMVC+hibernate 这种情况 由于 hibernate 的 懒汉机制,和 Spring 事务机制(不确定)造成的 由于 spring 配置的时候,在service 层 ...
- css浮动与绝对定位小记
浮动 float属性可以设置的值为none,left,right.对于设置了浮动的元素,会向其父元素的左侧或右侧紧靠,默认情况下,盒子的宽度不再伸展,而是根据盒子里面的内容来确定.浮动可以让一个元素移 ...
- linux 下的信号量参数
linux 下的信号量参数 转载自:http://blog.itpub.net/26110315/viewspace-718306/ 信号量是一种锁机制用于协调进程之间互斥的访问临界资源.以确保某种共 ...
- 基于spring和Quartz定时器
最近做一个小项目,要每7天去调用webservice获取一次数据.所以就用定时器来完成spring是4.1.6,quartz是2.2.1. 首先配置spring的xml文件.首先定义你要被执行的类 & ...
- C#语法糖,让编程更具乐趣
一.什么是语法糖 语法糖(Syntactic sugar),也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法 ...
- JAVA异常处理机制的简单原理和应用
- lua 和 c/c++ 交互 (持续更新)
参考: http://blog.csdn.net/xiaohuh421/article/details/7476485 http://blog.csdn.net/shun_fzll/article/d ...
- metaclass 常用方式
一个类作为metaclass的时候,我们需要重写它的__new__方法,这个方法的参数包括要创建class object的 metaclass,类名,父类集合,类成员 class MyMetaclas ...
- 从零开始学习Android(一)Android环境的搭建
好久没有开始写博客了,最近开始学习Android,所以想把学习的笔记都一一记录下来.一来是方便自己以后资料的查询,其次也是给Android新手朋友进行学习使用,再次也希 望得到高手的指点.废话少说,我 ...