博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TestNG之注解的生命周期
阅读量:6487 次
发布时间:2019-06-24

本文共 4936 字,大约阅读时间需要 16 分钟。

有必要介绍一下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");    }}

运行后的结果:

beforesuitebeforeTestbeforeclass's TestNG2TestNG2's beforemethodTestNG2's testt1TestNG2's aftermethodTestNG2's beforemethodTestNG2's testt2TestNG2's aftermethodafterclass's TestNG2aftertestaftersuite

由此可见,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中这样配置

运行的结果是:

beforesuitebeforeTestbeforeclass's TestNG1TestNG1's test3TestNG1's test4afterclass's TestNG1beforeclass's TestNG2TestNG2's beforemethodTestNG2's testt1TestNG2's aftermethodTestNG2's beforemethodTestNG2's testt2TestNG2's aftermethodafterclass's TestNG2aftertestaftersuite

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

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

画了个路程图更直接点。

转载地址:http://tnauo.baihongyu.com/

你可能感兴趣的文章
【云栖精选】《云栖精选阿里巴巴技术实战2016年刊》重磅发布
查看>>
Javascript:谈谈JS的全局变量跟局部变量
查看>>
MonoDevelop的app.config问题
查看>>
if continue 语句
查看>>
When you are old (当你老了)
查看>>
SAE+Servlet+JSP实现微信公众平台OAuth2.0网页授权的使用
查看>>
Fast TileMap
查看>>
Problem25
查看>>
软件项目进度控制要处理好的四个基本问题(转)
查看>>
iOS沙盒目录结构
查看>>
通过IMAP定向收取网易邮箱邮件(疑难)
查看>>
ARTS 第一周
查看>>
【PPT已更新】给开发者的区块链技术指南 | JTalk 掘金线下活动第五期
查看>>
HTML标签 转 NSAttributedString
查看>>
vue实现根据多选框按钮,动态给百度地图添加和删除相应的覆盖物
查看>>
前奏交响曲
查看>>
深入理解Spring系列之七:web应用自动装配Spring配置
查看>>
axios插件学习
查看>>
用python手刃Leetcode(9):回文数【简单题】
查看>>
SpringFlux入门(上篇)
查看>>