有必要介绍一下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.

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

  1. package com.test;
  2.  
  3. import org.testng.annotations.AfterClass;
  4. import org.testng.annotations.AfterGroups;
  5. import org.testng.annotations.AfterMethod;
  6. import org.testng.annotations.AfterSuite;
  7. import org.testng.annotations.AfterTest;
  8. import org.testng.annotations.BeforeClass;
  9. import org.testng.annotations.BeforeGroups;
  10. import org.testng.annotations.BeforeMethod;
  11. import org.testng.annotations.BeforeSuite;
  12. import org.testng.annotations.BeforeTest;
  13. import org.testng.annotations.Test;
  14.  
  15. /**
  16. * @author QiaoJiafei
  17. * @version 创建时间:2016年3月24日 下午9:21:00
  18. * 类说明
  19. */
  20. public class TestNG2 {
  21. @BeforeSuite
  22. public void beforesuite() {
  23. System.out.println("beforesuite");
  24. }
  25. @AfterSuite
  26. public void aftersuite() {
  27. System.out.println("aftersuite");
  28. }
  29.  
  30. @BeforeTest
  31. public void beforetest() {
  32. System.out.println("beforeTest");
  33. }
  34. @AfterTest
  35. public void AfterTest() {
  36. System.out.println("aftertest");
  37. }
  38.  
  39. @BeforeClass
  40. public void beforeclass() {
  41. System.out.println("beforeclass's TestNG2");
  42. }
  43.  
  44. @AfterClass
  45. public void aftertclass() {
  46. System.out.println("afterclass's TestNG2");
  47. }
  48.  
  49. @BeforeMethod
  50. public void beforemethod() {
  51. System.out.println("TestNG2's beforemethod");
  52. }
  53.  
  54. @AfterMethod
  55. public void aftertmethod() {
  56. System.out.println("TestNG2's aftermethod");
  57. }
  58.  
  59. @BeforeGroups
  60. public void beforegroups() {
  61. System.out.println("TestNG2's beforegroups");
  62. }
  63.  
  64. @AfterGroups
  65. public void aftergroups() {
  66. System.out.println("TestNG2's aftergroups");
  67. }
  68.  
  69. @Test
  70. public void test1() {
  71. System.out.println("TestNG2's testt1");
  72. }
  73.  
  74. @Test(groups="gr")
  75. public void test2() {
  76. System.out.println("TestNG2's testt2");
  77. }
  78.  
  79. public void ff() {
  80. System.out.println("nothing");
  81. }
  82. }

运行后的结果:

  1. beforesuite
  2. beforeTest
  3. beforeclass's TestNG2
  4. TestNG2's beforemethod
  5. TestNG2's testt1
  6. TestNG2's aftermethod
  7. TestNG2's beforemethod
  8. TestNG2's testt2
  9. TestNG2's aftermethod
  10. afterclass's TestNG2
  11. aftertest
  12. aftersuite

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

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

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

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

  1. package com.test;
  2.  
  3. import org.testng.annotations.AfterClass;
  4. import org.testng.annotations.BeforeClass;
  5. import org.testng.annotations.Test;
  6.  
  7. /**
  8. * @author QiaoJiafei
  9. * @version 创建时间:2016年3月24日 下午9:20:47
  10. * 类说明
  11. */
  12. public class TestNG1 {
  13.  
  14. @BeforeClass
  15. public void beforeclass() {
  16. System.out.println("beforeclass's TestNG1");
  17. }
  18.  
  19. @AfterClass
  20. public void afterclass() {
  21. System.out.println("afterclass's TestNG1");
  22. }
  23.  
  24. @Test
  25. public void test3() {
  26. System.out.println("TestNG1's test3");
  27. }
  28. @Test(groups="haha")
  29. public void test4() {
  30. System.out.println("TestNG1's test4");
  31. }
  32.  
  33. }

XML中这样配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <suite name="Suite" parallel="false">
  3. <test name="Test">
  4. <classes>
  5. <class name="com.test.TestNG1"/>
  6. <class name="com.test.TestNG2"/>
  7. </classes>
  8. <!-- <groups>
  9. <run>
  10. <include name="gr" />
  11. </run>
  12. </groups>-->
  13. </test> <!-- Test -->
  14. </suite> <!-- Suite -->

运行的结果是:

  1. beforesuite
  2. beforeTest
  3. beforeclass's TestNG1
  4. TestNG1's test3
  5. TestNG1's test4
  6. afterclass's TestNG1
  7. beforeclass's TestNG2
  8. TestNG2's beforemethod
  9. TestNG2's testt1
  10. TestNG2's aftermethod
  11. TestNG2's beforemethod
  12. TestNG2's testt2
  13. TestNG2's aftermethod
  14. afterclass's TestNG2
  15. aftertest
  16. 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. sDashboard:简单的,轻量级的 jQuery 仪表板插件

    sDashboard 是一个轻量的仪表板 jQuery 插件,转换一个对象数组到仪表板.数组中的每个对象将被呈现为一个仪表板组件,可以通过左右拖 ​动重新排列. sDashboards 内置渲染 Da ...

  2. 最简单的轮播广告(原生JS)

    改变每个图片的opacity属性:来自学友刘斌 素材图片: <!DOCTYPE html> <html lang="en"> <head> &l ...

  3. EXCEL经纬度转化

    =LEFT(A1,FIND("°",A1)-1)+MID(A1,FIND("°",A1)+1,FIND(CHAR(39),A1)-1-FIND("°& ...

  4. Office 365 - SharePoint 2013 Online 在应用商店中添加应用

    1.在使用应用程序商店的时候,先点击配置应用商店设置,如下图: 2.发现SharePoint要求我们创建应用程序目录,用来分发SharePoint App的一个网站,不过不创建的话,依然可以在应用商店 ...

  5. CAML获取SharePoint文档库中除文件夹外所有文档

    方法一: <QueryOptions> <ViewAttributes Scope="Recursive" /> </QueryOptions> ...

  6. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q40-Q44)

    Question 40 You have a social networking site in SharePoint 2010 that allows users to post content f ...

  7. Android存储访问及目录

    Android存储访问及目录 Android的外部存储 Android支持外部存储(case-insensitive filesystem with immutable POSIX permissio ...

  8. Android 插件化

    1.介绍 涛哥写的文章  <Android 插件化的 过去 现在 未来> http://kymjs.com/code/2016/05/04/01#rd

  9. iOS开源项目MobileProject功能点介绍

    一:MobileProject简介 MobileProject项目是一个以MVC模式搭建的开源功能集合,基于Objective-C上面进行编写,意在解决新项目对于常见功能模块的重复开发,MobileP ...

  10. 【代码笔记】iOS-电影上的花絮,自动滚动

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...