有必要介绍一下TestNG注解的生命周期,先看一下官网支持的注解有

@BeforeSuite
@AfterSuite
@BeforeTest
@AfterTest
@BeforeGroups
@AfterGroups
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
Configuration information for a TestNG class:

@BeforeSuite: The annotated method will be run before all tests in this suite have run. 
@AfterSuite: The annotated method will be run after all tests in this suite have run. 
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. 
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run. 
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. 
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. 
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked. 
@AfterClass: The annotated method will be run after all the test methods in the current class have been run. 
@BeforeMethod: The annotated method will be run before each test method. 
@AfterMethod: The annotated method will be run after each test method.

英文看到不是很明白,那么我们从挨个实验。

package com.test;import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;/** * @author QiaoJiafei * @version 创建时间:2016年3月24日 下午9:21:00 * 类说明 */
public class TestNG2 {@BeforeSuitepublic void beforesuite() {System.out.println("beforesuite");}@AfterSuitepublic void aftersuite() {System.out.println("aftersuite");}@BeforeTestpublic void beforetest() {System.out.println("beforeTest");}@AfterTestpublic void AfterTest() {System.out.println("aftertest");}@BeforeClasspublic void beforeclass() {System.out.println("beforeclass's TestNG2");}@AfterClasspublic void aftertclass() {System.out.println("afterclass's TestNG2");}@BeforeMethodpublic void beforemethod() {System.out.println("TestNG2's beforemethod");}@AfterMethodpublic void aftertmethod() {System.out.println("TestNG2's aftermethod");}@BeforeGroupspublic void beforegroups() {System.out.println("TestNG2's beforegroups");}@AfterGroupspublic void aftergroups() {System.out.println("TestNG2's aftergroups");}@Testpublic void test1() {System.out.println("TestNG2's testt1");}@Test(groups="gr")public void test2() {System.out.println("TestNG2's testt2");}public void ff() {System.out.println("nothing");}
}

运行后的结果:

beforesuite
beforeTest
beforeclass's TestNG2
TestNG2's beforemethod
TestNG2's testt1
TestNG2's aftermethod
TestNG2's beforemethod
TestNG2's testt2
TestNG2's aftermethod
afterclass's TestNG2
aftertest
aftersuite

由此可见,testng运行时,顺序是这样的:

@BeforeSuite->@BeforeTest->@BeforeClass->{@BeforeMethod->@Test->@AfterMethod}->@AfterClass->@AfterTest->@AfterSuite

其中{}内的与多少个@Test,就循环执行多少次。

我们知道了在一个类中注解的生命周期,那么这些注解的作用范围呢,下面我们再建一个类

package com.test;import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;/** * @author QiaoJiafei * @version 创建时间:2016年3月24日 下午9:20:47 * 类说明 */
public class TestNG1 {@BeforeClasspublic void beforeclass() {System.out.println("beforeclass's TestNG1");}@AfterClasspublic void afterclass() {System.out.println("afterclass's TestNG1");}@Testpublic void test3() {System.out.println("TestNG1's test3");}@Test(groups="haha")public void test4() {System.out.println("TestNG1's test4");}}

XML中这样配置

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false"><test name="Test"><classes><class name="com.test.TestNG1"/><class name="com.test.TestNG2"/></classes><!--   <groups><run><include name="gr" /></run></groups>--></test> <!-- Test -->
</suite> <!-- Suite -->

运行的结果是:

beforesuite
beforeTest
beforeclass's TestNG1
TestNG1's test3
TestNG1's test4
afterclass's TestNG1
beforeclass's TestNG2
TestNG2's beforemethod
TestNG2's testt1
TestNG2's aftermethod
TestNG2's beforemethod
TestNG2's testt2
TestNG2's aftermethod
afterclass's TestNG2
aftertest
aftersuite

看到没有,除了@BeforeSuite、@BeforeTest、@AfterTest、@AfterSuite可以对不同的测试类生效外,其他的注解的作用范围只在本类中生效。这样就可以清晰的知道什么样的逻辑应该放在哪个注解中,如只想在测试中只启动、关闭一次浏览器,且再不同的测试类中共用,那么我们就可以把启动、关闭浏览器的方法放在suite和test中

至于@BeforeGroups和@AfterGroups笔者目前还没有发现怎么生效。

画了个路程图更直接点。

TestNG之注解的生命周期相关推荐

  1. Lifecycle Activity和Fragment生命周期感知组件 LifecycleObserver MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章

    前言 当你准备去复习Spring中Bean的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...

  3. 【spring学习】Spring bean生命周期及注解

    Spring Spring是分层的javaSE/EE轻量级开源框架,以IOC(控制翻转)和AOP(面向切面编程)为内核 分层:web层:SpringMvc持久层:JDBC Template业务层:Sp ...

  4. Spring框架学习day_02:组件扫描 / 注解内部读解 / 组件扫描中配置作用域和生命周期 / 解耦 / 自动装配(两种方式) / 读取文件

    1. 组件扫描 首先,必须让Spring扫描组件所在的包,并且,组件类的声明之前必须添加@Component注解! 其实,除了@Component注解以外,还可以使用以下注解实现同样的效果: @Con ...

  5. Spring-基于注解的配置[03Bean作用范围和生命周期方法]

    Bean的作用范围 实例 Bean的生命周期方法 实例 Bean的作用范围 通过注解配置的Bean和通过<bean>配置的Bean一样,默认的作用范围都是singleton. Spring ...

  6. 【Spring注解系列10】SpringBean的生命周期

    1.SpringBean生命周期定义 指bean创建---初始化----销毁的过程. 构造(对象创建): 单实例:在容器启动的时候创建对象 多实例:在每次获取的时候创建对象 初始化和销毁方式: 指定初 ...

  7. java成员属性生命周期,Spring注解 - 生命周期、属性赋值、自动装配

    一.Bean的生命周期 流程 Bean创建 -- 初始化 -- 销毁 创建: 单实例:在容器启动时创建对象 多实例:每次调用时创建对象 初始化: 都是在对象创建完成后,调用初始化方法 销毁: 单实例: ...

  8. spring系列-注解驱动原理及源码-bean生命周期

    目录 一.Bean的初始化和销毁 1.@Bean指定初始化和销毁方法 2.通过实现InitializingBean和Disposabean来初始化和销毁bean 3.使用JSR250定义的@PostC ...

  9. Servlet和HTTP请求协议-学习笔记01【Servlet_快速入门-生命周期方法、Servlet_3.0注解配置、IDEA与tomcat相关配置】

    Java后端 学习路线 笔记汇总表[黑马程序员] Servlet和HTTP请求协议-学习笔记01[Servlet_快速入门-生命周期方法.Servlet_3.0注解配置.IDEA与tomcat相关配置 ...

最新文章

  1. 牛客网暑期ACM多校训练营(第二场)J farm (二维树状数组)
  2. leetcode145. 二叉树的后序遍历(dfs)
  3. 【AI视野·今日CV 计算机视觉论文速览 第194期】Mon, 10 May 2021
  4. day21—AngularJS学习初体验
  5. spring-security过程分析
  6. java jpanel 叠加_java之JFrame、JPanel混合布局-Go语言中文社区
  7. 地统计学插值 | IDW(反距离)和Kriging(克里金)
  8. C++ 获取鼠标点击位置
  9. JLink驱动设备管理器中显示黄色感叹号
  10. idea打包 jar文件
  11. Gartner:细分分析如何提高销售预测的准确性?
  12. Red5 流媒体技术(初级了解)
  13. 对马哥教育的第一周感悟
  14. 国美易卡的标识符和关键字是什么(国美易卡)
  15. 行列式用计算机怎么算,行列式计算机方法
  16. stm32h7内存分配_【STM32H7教程】第9章 STM32H7重要知识点数据类型,变量和堆栈...
  17. Centos 安装nvidia和cuda驱动
  18. fastlane 上传到蒲公英和Fir 配置
  19. 物理学专业英语(词汇整理)--------03
  20. java中定义变量名时大写_,【Java】变量命名规范

热门文章

  1. Redis -基础命令
  2. 在矩阵上跑最小生成树
  3. 使用WGAN生成手写字体
  4. 高性能服务器开发之C++定时器
  5. 基于图像切换器(imageSwitcher)的支持动画的图片浏览器
  6. JAVA程序通过JNI调用C/C++库
  7. Spring 3.2.* MVC通过Ajax获取JSON数据报406错误
  8. 考系统架构设计师必看--《软件工程》网页教程
  9. 用js控制选择CheckBoxList
  10. POJ3398 Perfect Service