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. kali 系列学习03 - 主动扫描

    主动扫描首先考虑使用代理IP保护自己,其次掌握 Nmap 工具使用 第一部分 扫描对方时,最好隐藏一下自己root@kali:/etc# service tor statusUnit tor.serv ...

  2. springboot中aop的使用

    Spring AOP(Aspect Oriented Programming),即面向切面编程,是OOP(Object Oriented Programming,面向对象编程)的补充和完善. OOP引 ...

  3. MathType颜色设置的技巧

    MathType功能非常强大,在编辑公式时使用非常方便.运用MathType不仅可以改变公式的字体和字号,也可以改变公式字体颜色,MathType颜色设置还是有一套技术的,下面我们就一起来看看公式编辑 ...

  4. Win10访问Ubuntu的samba共享文件

    大致分为以下几个步骤: 一.开启samba服务器 二.配置共享目录和用户权限 三.开启samba客户端 四.访问共享目录 一:开启samba服务器 安装samba服务器:  sudo apt-get ...

  5. 记XShell无法连接虚拟机中的Linux,但Linux系统中可以连接外网。

    如题. 原本设置如下: 本地机IP 为192.168.43.XXX VMWare中"虚拟机网络编辑器"中子网设置为192.168.39.0. 虚拟机中IP为192.168.39.1 ...

  6. 牛客练习赛71 数学考试 题解(dp)

    题目链接 题目大意 要你求出有多少个长度为n的排列满足m个限制条件 第i个限制条件 p[i]表示前 p[i]个数不能是1-p[i]的排列 题目思路 这个感觉是dp但是不知道怎么dp 首先就是要明白如果 ...

  7. Java集合【4】-- iterable和Iterator的异同分析详解

    目录 一.iterator介绍 二.iterable接口 三.为什么有Iterator还需要Iterable 一.iterator介绍 iterator接口,也是集合大家庭中的一员.和其他的Map和C ...

  8. python -- 对list去重并找出列表list中的重复元素

    一.一个列表中可能含有重复元素,使用set()可以实现列表的去重处理,但是无法知道哪些元素是重复的,下面的函数用于找出哪些元素重复了,以及重复的次数. 代码: from collections imp ...

  9. 团队作业第六次——Beta冲刺

    这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 团队进行Beta冲刺 作业正文 正文 其他参考文献 无 代码规范与计划 代码 ...

  10. Spring Boot系列 八、集成Kafka

    一.引入依赖 <dependency> <groupId>org.springframework.kafka</groupId> <artifactId> ...