有必要介绍一下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 {
@BeforeSuite
public void beforesuite() {
System.out.println("beforesuite");
}
@AfterSuite
public void aftersuite() {
System.out.println("aftersuite");
} @BeforeTest
public void beforetest() {
System.out.println("beforeTest");
}
@AfterTest
public void AfterTest() {
System.out.println("aftertest");
} @BeforeClass
public void beforeclass() {
System.out.println("beforeclass's TestNG2");
} @AfterClass
public void aftertclass() {
System.out.println("afterclass's TestNG2");
} @BeforeMethod
public void beforemethod() {
System.out.println("TestNG2's beforemethod");
} @AfterMethod
public void aftertmethod() {
System.out.println("TestNG2's aftermethod");
} @BeforeGroups
public void beforegroups() {
System.out.println("TestNG2's beforegroups");
} @AfterGroups
public void aftergroups() {
System.out.println("TestNG2's aftergroups");
} @Test
public 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 { @BeforeClass
public void beforeclass() {
System.out.println("beforeclass's TestNG1");
} @AfterClass
public void afterclass() {
System.out.println("afterclass's TestNG1");
} @Test
public 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. Java Servlet详解(体系结构+注解配置+生命周期)

    Java Servlet详解(注解配置+生命周期) 什么是Servlet : (Server applet)? 顾名思义:服务端的小程序 Servlet只是一个接口,定义了Java被浏览器访问到(To ...

  2. spring注解-bean生命周期

    https://www.jianshu.com/p/70b935f2b3fe bean的生命周期 bean创建---初始化----销毁的过程 容器管理bean的生命周期 对象创建:容器启动后调用bea ...

  3. Jetpack 架构组件 Lifecycle 生命周期 MD

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

  4. 对Rust所有权、借用及生命周期的理解

    Rust的内存管理中涉及所有权.借用与生命周期这三个概念,下面是个人的一点粗浅理解. 一.从内存安全的角度理解Rust中的所有权.借用.生命周期 要理解这三个概念,你首要想的是这么做的出发点是什么-- ...

  5. 17、生命周期-BeanPostProcessor在Spring底层的使用

    17.生命周期-BeanPostProcessor在Spring底层的使用 bean赋值.注入其他组件.@Autowired注解.生命周期注解.@Async --都是 BeanPostProcesso ...

  6. rust 函数-生命周期

    记录一下自己理解的生命周期. 每个变量都有自己的生命周期. 在c++里生命周期好比作用域, 小的作用域的可以使用大作用域的变量. 如果把这里的每个作用域取个名,那么就相当于rust里的生命周期注解. ...

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

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

  8. Servlet生命周期和注解配置

    Servlet的生命周期和注解配置问题 /* Servlet? 运行在服务器上的小程序 定义浏览器访问到Tomcat的规则 一.生命周期? 1.创建 2.提供服务 3.被销毁 二.servlet3.0 ...

  9. spring(二、bean生命周期、用到的设计模式、常用注解)

    spring(二.bean生命周期.用到的设计模式.常用注解) Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的. ...

随机推荐

  1. Masonry – 实现 Pinterest 模式的网格砌体布局

    Masonry 是一款 JavaScript 网格布局插件,可以实现类似 Pinterest 模式的砌体布局效果.通过把元素自动填充在垂直的空白区域,就像墙上堆砌的石头一样.这个库还可以作为 jQue ...

  2. 解决html表格中内容超出不强制换行和超出宽度自动隐藏并显示省略号

    在表格布局中经常会遇到因为表格内容长短的变化导致布局混乱的情况,这个时候我们可能会有为了布局稳定把单元格宽度写死的情况:但是我们设置了宽度却发现超出了宽度之后会自动变大,用css定义元素的overfl ...

  3. 探究TCP

    OSI OSI是Open System Interconnection的缩写,意为开放式系统互联.国际标准化组织(ISO)制定了OSI模型,该模型定义了不同计算机互联的标准,是设计和描述计算机网络通信 ...

  4. 对于Access数据库查询遇到空值的解决办法

    1.Access数据库在office环境下对于null是识别的,但是,在开发环境下,Access数据库对于where xxx is null是不识别的. 2.查询空值解决办法:select * fro ...

  5. Android Animation学习(五) ApiDemos解析:容器布局动画 LayoutTransition

    Android Animation学习(五) ApiDemos解析:容器布局动画 LayoutTransition Property animation系统还提供了对ViewGroup中的View改变 ...

  6. gif动图快速制作方法(附工具)

    现在写博客或是wiki的过程中,会经常引用到图片,特别是客户端经常与页面相关所以截图不可避.但是越来越多的效果仅仅一张图片是无法清楚的描述.并且博客或是wiki也是支持gif图的.gif图的制作方法有 ...

  7. UITextFiled,UITextView长度限制

    长度限制用到的地方很多,但是需求都不一样.有的要求全部字符按一个处理,有的要求英文字母按一个,中文按两个,emoji按四个.这样就会遇到各种各样奇怪的问题,再被虐了无数次后,终于解决掉了.下面就来写写 ...

  8. 私有Pods封装个推SDK功能(解决方案)

    一:运用场景 公司中同时有好几个APP在开发,而且每个APP都有使用到集成个推SDK来处理消息的功能,以前的做法是每个APP都去集成并在AppDelegate处理一些SDK的代码,包含个推基础配置.消 ...

  9. 【原】Github系列之一:一起做仿天气类应用中的实时模糊效果LiveBlur

    从本文开始,我将专门开辟一个Github Code系列,开源自己写的一部分有意思而且实用的demo,共同学习.以前都发布在git OSChina上,后面有空会陆陆续续整理到Github上.OSChin ...

  10. SQL Server 分区表补充说明

    分区教程参阅:http://database.9sssd.com/mssql/art/951 切换分区(归档):http://technet.microsoft.com/zh-cn/library/m ...