Before类别和After类别注解

举例说明

创建两个TestNGAnnotationTest.java和TestNGAnnotationTest2.java的类

TestNGAnnotationTest.java

package com.lc.testngTest;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; public class TestNGAnnotationTest { @BeforeSuite
public void beforeSuite() { System.out.println(this.getClass().getName() + " beforeSuite"); } @AfterSuite
public void afterSuite() { System.out.println(this.getClass().getName() + " afterSuite"); } @BeforeTest
public void beforeTest() { System.out.println(this.getClass().getName() + " beforeTest"); } @AfterTest
public void afterTest() { System.out.println(this.getClass().getName() + " afterTest"); } @BeforeClass
public void beforeClass() { System.out.println(this.getClass().getName() + " beforeClass"); } @AfterClass
public void afterClass() { System.out.println(this.getClass().getName() + " afterClass"); } @BeforeMethod
public void beofreMethod() { System.out.println(this.getClass().getName() + " beforeMethod"); } @AfterMethod
public void afterMethod() { System.out.println(this.getClass().getName() + " afterMethod"); } @Test
public void test1() { System.out.println(this.getClass().getName() + " test1"); } @Test
public void test2() { System.out.println(this.getClass().getName() + " test2"); } }

TestNGAnnotationTest2.java

package com.lc.testngTest;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; public class TestNGAnnotationTest2 { @BeforeSuite
public void beforeSuite() { System.out.println(this.getClass().getName() + " beforeSuite"); } @AfterSuite
public void afterSuite() { System.out.println(this.getClass().getName() + " afterSuite"); } @BeforeTest
public void beforeTest() { System.out.println(this.getClass().getName() + " beforeTest"); } @AfterTest
public void afterTest() { System.out.println(this.getClass().getName() + " afterTest"); } @BeforeClass
public void beforeClass() { System.out.println(this.getClass().getName() + " beforeClass"); } @AfterClass
public void afterClass() { System.out.println(this.getClass().getName() + " afterClass"); } @BeforeMethod
public void beofreMethod() { System.out.println(this.getClass().getName() + " beforeMethod"); } @AfterMethod
public void afterMethod() { System.out.println(this.getClass().getName() + " afterMethod"); } @Test
public void test1() { System.out.println(this.getClass().getName() + " test1"); } @Test
public void test2() { System.out.println(this.getClass().getName() + " test2"); } }

创建运行项目的xml文件

testng2.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="suite1" verbose="1" >
<test name="test1">
<classes>
<class name="com.lc.testngTest.TestNGAnnotationTest"></class>
<class name="com.lc.testngTest.TestNGAnnotationTest2"></class>
</classes>
</test> </suite>

运行结果

[TestNG] Running:
D:\workspace\webTestNG\WebContent\testng2.xml com.lc.testngTest.TestNGAnnotationTest beforeSuite
com.lc.testngTest.TestNGAnnotationTest2 beforeSuite
com.lc.testngTest.TestNGAnnotationTest beforeTest
com.lc.testngTest.TestNGAnnotationTest2 beforeTest
com.lc.testngTest.TestNGAnnotationTest beforeClass
com.lc.testngTest.TestNGAnnotationTest beforeMethod
com.lc.testngTest.TestNGAnnotationTest test1
com.lc.testngTest.TestNGAnnotationTest afterMethod
com.lc.testngTest.TestNGAnnotationTest beforeMethod
com.lc.testngTest.TestNGAnnotationTest test2
com.lc.testngTest.TestNGAnnotationTest afterMethod
com.lc.testngTest.TestNGAnnotationTest afterClass
com.lc.testngTest.TestNGAnnotationTest2 beforeClass
com.lc.testngTest.TestNGAnnotationTest2 beforeMethod
com.lc.testngTest.TestNGAnnotationTest2 test1
com.lc.testngTest.TestNGAnnotationTest2 afterMethod
com.lc.testngTest.TestNGAnnotationTest2 beforeMethod
com.lc.testngTest.TestNGAnnotationTest2 test2
com.lc.testngTest.TestNGAnnotationTest2 afterMethod
com.lc.testngTest.TestNGAnnotationTest2 afterClass
com.lc.testngTest.TestNGAnnotationTest afterTest
com.lc.testngTest.TestNGAnnotationTest2 afterTest
com.lc.testngTest.TestNGAnnotationTest afterSuite
com.lc.testngTest.TestNGAnnotationTest2 afterSuite ===============================================
suite1
Total tests run: 4, Failures: 0, Skips: 0
===============================================

其中beforeSuite,afterSuite和beforeTest,afterTest区分不明显,下面修改xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="suite1" verbose="1" >
<test name="test1">
<classes>
<class name="com
[TestNG] Running:
D:\workspace\webTestNG\WebContent\testng.xml com.lc.testngTest.TestNGAnnotationTest beforeSuite
com.lc.testngTest.TestNGAnnotationTest2 beforeSuite
com.lc.testngTest.TestNGAnnotationTest beforeTest
com.lc.testngTest.TestNGAnnotationTest beforeClass
com.lc.testngTest.TestNGAnnotationTest beforeMethod
com.lc.testngTest.TestNGAnnotationTest test1
com.lc.testngTest.TestNGAnnotationTest afterMethod
com.lc.testngTest.TestNGAnnotationTest beforeMethod
com.lc.testngTest.TestNGAnnotationTest test2
com.lc.testngTest.TestNGAnnotationTest afterMethod
com.lc.testngTest.TestNGAnnotationTest afterClass
com.lc.testngTest.TestNGAnnotationTest afterTest
com.lc.testngTest.TestNGAnnotationTest2 beforeTest
com.lc.testngTest.TestNGAnnotationTest2 beforeClass
com.lc.testngTest.TestNGAnnotationTest2 beforeMethod
com.lc.testngTest.TestNGAnnotationTest2 test1
com.lc.testngTest.TestNGAnnotationTest2 afterMethod
com.lc.testngTest.TestNGAnnotationTest2 beforeMethod
com.lc.testngTest.TestNGAnnotationTest2 test2
com.lc.testngTest.TestNGAnnotationTest2 afterMethod
com.lc.testngTest.TestNGAnnotationTest2 afterClass
com.lc.testngTest.TestNGAnnotationTest2 afterTest
com.lc.testngTest.TestNGAnnotationTest afterSuite
com.lc.testngTest.TestNGAnnotationTest2 afterSuite ===============================================
suite1
Total tests run: 4, Failures: 0, Skips: 0
===============================================

.lc.testngTest.TestNGAnnotationTest"></class>
</classes>
</test> <test name="test2">
<classes>
<class name="com.lc.testngTest.TestNGAnnotationTest2"></class>
</classes>
</test>
</suite>

运行结果

三、TestNG的基本注解(1)的更多相关文章

  1. TestNG学习-002-annotaton 注解概述及其执行顺序

    此文主要讲述用 TestNG 基础的 annotation (注解)知识,及其执行的顺序,并通过一个 TestNG 简单的实例演示 annotation 的执行顺序. 希望能对初学 TestNG 测试 ...

  2. asp.net mvc3 数据验证(三)—自定义数据注解

    原文:asp.net mvc3 数据验证(三)-自定义数据注解         前两节讲的都是asp.net mvc3预先设定的数据注解,但是系统自由的数据注解肯定不适合所有的场合,所以有时候我们需要 ...

  3. DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描

    DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许禁止转载    系列目录连接 DB数据源之Spr ...

  4. Spring Boot入门(三):使用Scheduled注解实现定时任务

    在程序开发的过程中,经常会使用定时任务来实现一些功能,比如: 系统依赖于外部系统的非核心数据,可以定时同步 系统内部一些非核心数据的统计计算,可以定时计算 系统内部的一些接口,需要间隔几分钟或者几秒执 ...

  5. Android AOP之路三 Android上的注解

    一.简单介绍 啥是注解.不懂的能够先看我上一篇文章. 在android 里面 注解主要用来干这么几件事: 和编译器一起给你一些提示警告信息. 配合一些ide 能够更加方便快捷 安全有效的编写java代 ...

  6. testng 的常用注解

    常用注解如下: @BeforeSuite: 此注解的方法会在当前测试集合中的任一测试用例前执行 @AfterSuite: 此注解的方法会在当前测试集合中的所有测试程序结束后执行 @BeforeTest ...

  7. TestNG的常用注解

    @BeforeSuite:表示此注解的方法会在当前测试集合(Suite)中的任一测试用例开始运行之前执行 @AfterSuite:表示此注解的方法会在当前测试集合(Suite)中的所有测试程序运行结束 ...

  8. spring boot: Bean的初始化和销毁 (一般注入说明(三) AnnotationConfigApplicationContext容器 JSR250注解)

    import org.springframework.context.annotation.AnnotationConfigApplicationContext; 使用AnnotationConfig ...

  9. 关于整合spring+mybatis 第三种方式-使用注解

    使用注解 1.与前两种方法一致.不过稍许不同的是beans.xml中配置的差异. <!-- 配置sqlSessionFactory --> <bean id="sqlSes ...

随机推荐

  1. 解决Ubuntu配置nginx出现的问题

    Ubuntu18.04配置nginx出现的各种错误 缺少pcre库 编译nginx 出现错误 安装pcre库,出现错误 手动编译安装pcre库 (1)下载并解压pcre库 wget https://f ...

  2. 快速安装jumpserver开源堡垒机

    一 安装centos 7.X操作系统 二.选择极速安装(注意配置需要是4G2核) https://jumpserver.readthedocs.io/zh/master/install/setup_b ...

  3. 对于线程池ThreadPool的学习总结

    线程池:就是一个管理线程的池子. 优点: 它帮我们管理线程,避免增加创建线程和销毁线程的资源损耗.因为线程其实也是一个对象,创建一个对象,需要经过类加载过程,销毁一个对象,需要走GC垃圾回收流程,都是 ...

  4. redis的分布式锁工具LockUtil

    /** * 基于redis的分布式锁工具 * * @author yuyufeng * */ public class LockUtil { // 获取redis static JedisPool j ...

  5. 深入浅出!springboot从入门到精通,实战开发全套教程!

    前言 之前一直有粉丝想让我出一套springboot实战开发的教程,我这边总结了很久资料和经验,在最近总算把这套教程的大纲和内容初步总结完毕了,这份教程从springboot的入门到精通全部涵盖在内, ...

  6. MindManager 2021 版新增了哪些功能

    MindManager Windows 21是一款强大的可视化工具和思维导图软件,在工作应用中有出色的表现.今天就带大家来看下这个新版本增加了哪些功能? 1.新增现代主题信息样式MindManager ...

  7. Linux禅道升级教程

    环境: centos7 禅道11.2升级道12.4 稳定版 下载: sudo wget https://www.zentao.net/dl/ZenTaoPMS.12.4.stable.zip 解压: ...

  8. Java基础教程——Jshell

    Jshell 从java9开始,java提供Jshell工具,可以输入代码片段并马上看到运行结果. 对于简单的Java语句测试,不需要新建文件,编译,运行了 Microsoft Windows [版本 ...

  9. 基础知识redis详解--【Foam番茄】

    Redis 学习方式: 上手就用 基本的理论先学习,然后将知识融汇贯通 nosql讲解 为什么要用Nosql 现在都是大数据时代 大数据一般的数据库无法进行分析处理了 至少要会Springboot+S ...

  10. 02_启动和销毁Service

    在Application关闭后,Service仍然会运行. package com.example.servdemo; import android.app.Activity; import andr ...