TestNG入门——注解之Before/After
注解是java 5新增的功能,可使用于类,方法,变量,testNG包提供的注解功能请见下表
1、@BeforeSuite or @AfterSuite 被注解的方法,将在整个测试套件之前 or 之后执行。
2、@BeforeTest or @AfterTest 被注解的方法,将在测试套件内所有用例执行之前 or 之后执行。
3、@BeforeGroups or @AfterGroups 被注解的方法,将在指定组内任意用例执行之前 or 之后执行。
4、@BeforeClass or @AfterClass 被注解的方法,将在此方法对应的类中的任意其他的,被标注为@Test 的方法执行前 or 执行后执行。
5、@BeforeMethod or@AfterMethod 被注解的方法,将在此方法对应的类中的任意其他的,被标注为@Test的方法执行前 or 执行后执行
6、@DataProvider 被注解的方法,强制返回一个 二维数组Object[ ][ ]作为另外一个@Test方法的数据工厂
7、@Factory 被注解的方法,作为对象工厂,强制返回一个对象数组 Object[ ]
8、@Listeners 定义一个测试类的监听器
9、@Parameters 定义一组参数,在方法运行期间向方法传递参数的值,参数的值在testng.xml中定义
10、@Test 标记方法为测试方法,如果标记的是类,则此类中所有的public方法都为测试方法
首先来看看以下几个注解再testNG中的使用方法与不同点
@BeforeSuite / @AfterSuite
@BeforeTest / @AfterTest
@BeforeGroups / @AfterGroups
@BeforeClass / @AfterClass
@BeforeMethod / @AfterMethod
创建一个java项目,项目结构如下:
TestClass.java文件中增加如下代码
- package test.beforafter;
- import org.testng.annotations.AfterClass;
- import org.testng.annotations.AfterTest;
- import org.testng.annotations.AfterGroups;
- import org.testng.annotations.AfterSuite;
- import org.testng.annotations.AfterMethod;
- import org.testng.annotations.BeforeClass;
- import org.testng.annotations.BeforeTest;
- import org.testng.annotations.BeforeGroups;
- import org.testng.annotations.BeforeSuite;
- import org.testng.annotations.BeforeMethod;
- import org.testng.annotations.Test;
- public class TestClass {
- @BeforeSuite
- public void beforeSuite(){
- System.out.println("Before Suite Method");
- }
- @AfterSuite
- public void afterSuite(){
- System.out.println("After Suite Method");
- }
- @BeforeTest
- public void beforeTest(){
- System.out.println("Before Test Method");
- }
- @AfterTest
- public void afterTest(){
- System.out.println("After Test Method");
- }
- @BeforeClass
- public void beforeClass(){
- System.out.println("Before Class Method");
- }
- @AfterClass
- public void afterClass(){
- System.out.println("After Class Method");
- }
- @BeforeGroups(groups={"testOne"})
- public void beforeGroupOne(){
- System.out.println("Before Group testOne Method");
- }
- @AfterGroups(groups={"testOne"})
- public void afterGroupOne(){
- System.out.println("After group testOne Method");
- }
- @BeforeGroups(groups={"testTwo"})
- public void beforeGroupTwo(){
- System.out.println("Before Group testTwo Method");
- }
- @AfterGroups(groups={"testTwo"})
- public void afterGroupTwo(){
- System.out.println("After Group testTwo Method");
- }
- @BeforeMethod
- public void beforeMethod(){
- System.out.println("Before Method");
- }
- @AfterMethod
- public void afterMethod(){
- System.out.println("After Method");
- }
- @Test(groups={"testOne"})
- public void testOne(){
- System.out.print("Test One Method");
- }
- @Test(groups={"testTwo"})
- public void testTwo(){
- System.out.print("Test Two Method");
- }
- @Test
- public void testThree(){
- System.out.println("Test Third Method");
- }
- }
alltestng.xml文件中增加如下配置
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- 只执行某个包 -->
- <suite name="Include Package Suite" verbose="1">
- <test name="Include Package Test">
- <packages>
- <package name="test.*">
- <include name="test.beforafter" />
- </package>
- </packages>
- </test>
- </suite>
执行后的顺序如下:
Before Suite Method
Before Test Method
Before Class Method
Before Group testOne Method
Before Method
Test One Method
After Method
After group testOne Method
Before Method
Test Third Method
After Method
Before Group testTwo Method
Before Method
Test Two Method
After Method
After Group testTwo Method
After Class Method
After Test Method
After Suite Method
TestNG入门——注解之Before/After的更多相关文章
- testng入门教程12 TestNG执行多线程测试
testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...
- TestNG 入门教程【转】
TestNG 入门教程[转] 国庆7天假期,大部分朋友都出去旅游了,微信圈里全是晒旅游的照片, 东南亚游,欧洲游呀,真是羡慕呀. 悲惨的我只去了上海野生动物园, 在家休息,利用这段假期,把之前学过的东 ...
- JAVA基础学习之IP简述使用、反射、正则表达式操作、网络爬虫、可变参数、了解和入门注解的应用、使用Eclipse的Debug功能(7)
1.IP简述使用//获取本地主机ip地址对象.InetAddress ip = InetAddress.getLocalHost();//获取其他主机的ip地址对象.ip = InetAddress. ...
- TestNG 入门指导——理解testng.xml执行/不执行某个包,某个类,某个方法
这一篇我们主要学习如下几个知识点: ⑴关于testng.xml ⑵创建一个测试套件 ⑶执行testng.xml ⑷在测试套件中创建多个测试用例 ⑸在用例中增加class,packages, metho ...
- testng入门教程16数据驱动(把数据写在xml)
testng入门教程16数据驱动(把数据写在xml) testng入门教程16数据驱动(把数据写在xml)把数据写在xml文件里面,在xml文件右键选择runas---testng执行 下面是case ...
- testng入门教程2用TestNG编写测试及执行测试
编写TestNG测试基本上包括以下步骤: 测试和编写业务逻辑,在代码中插入TestNG的注解.. 添加一个testng.xml文件或build.xml中在测试信息(例如类名,您想要运行的组,等..) ...
- TestNG入门到...
目录 一.概述 二.@Test注解常用参数 三.测试中常用的断言(assert) 四.TestNG常用注解及使用 五.配置文件xml常用标签 六.参数传递 七.测试报告 一.概述 1.TestNG是一 ...
- TestNG基本注解
TestNG的注解: 注解 描述 @BeforeSuite 注解的方法将只运行一次,运行所有测试前此套件中. @AfterSuite 注解的方法将只运行一次此套件中的所有测试都运行之后. @Befor ...
- 【转】TestNG常用注解
http://blog.csdn.net/d6619309/article/details/52435084 TestNG的注解大部分用在方法级别上.常用的注解列举如下: 1. Before类别和Af ...
随机推荐
- EF 通过导航添加数据
Fluent Api是指定模型与数据库表之间的对应关系 //一对多 this.HasOptional(x => x.主表).WithMany(x => x.多表).HasForeignKe ...
- EF Core 简单使用介绍
EF Core 是一个ORM(对象关系映射),它使 .NET 开发人员可以使用 .NET对象操作数据库,避免了像ADO.NET访问数据库的代码,开发者只需要编写对象即可. EF Core 支持多种数据 ...
- html5新增表单控件和表单属性
表单验证 Invalid事件 : 验证反馈 input.addEventListener('invalid',fn,false) 阻止默认验证:ev.preventDefault() formnova ...
- QT场景视图父子关系图元打印研究
在之前的一篇文章中,实现了QT场景视图的打印功能,主要通过render函数来实现,非常简单和方便. 在实际的项目需求中,除了打印整个场景外,还需要对单个图形进行打印操作,基于item的图形可以在pai ...
- scrapy设置logger日志
1.在settings中设置log级别,在settings.py中添加一行: LOG_LEVEL = 'WARNING' Scrapy提供5层logging级别: CRITICAL - 严重错误 ER ...
- python使用sched模块执行周期性任务和定时任务
执行周期性任务 sched模块是一个通用的事件调度程序,可以对任务进行延迟调度,基于此,可以用它来实现周期性任务. # coding:utf8 import time import sched # 初 ...
- 01、Linux基础命令
linux 一些主要目录的认识: /bin 二进制可执行命令 /boot 存放系统引导文件,如 内核.grub 等 /dev 设备文件 /etc 系统配置目录 /home 普通用户家目录 /lib 系 ...
- Java多线程编程核心技术-第4章-Lock的使用-读书笔记
第 4 章 Lock 的使用 本章主要内容 ReentrantLocal 类的使用. ReentrantReadWriteLock 类的使用. 4.1 使用 ReentrantLock 类 在 Jav ...
- 04-cmake语法-STREQUAL
STREQUAL 用于比较字符串,相同返回 true .
- pl/sql的tnsnames.ora文件配置
位置:D:\app\product\11.2.0\dbhome_1\network\admin\tnsnames.ora(根据安装位置具体情况而定) zx_U= (DESCRIPTION = ...