TestNG之注解的生命周期

有必要介绍一下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笔者目前还没有发现怎么生效。

画了个路程图更直接点。

posted on   乔叶叶  阅读(4948)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示