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 ...
随机推荐
- flask中的static_path和static_path_url和static_folder
static_folder表示静态文件所在路径,默认为root_dir下的static文件夹 static_url_path的行为比较复杂 如果static_folder未被指定(也就是默认值stat ...
- JS基础 —— 数据类型
JS数据类型分为简单数据类型(基本数据类型)和复杂数据类型(引用数据类型). 基本数据类型:Undefined.Null.Boolean.Number.String.Symbol. 引用数据类型:Ob ...
- 默认展开ztree树形菜单
var setting = { view: { selectedMulti: false //按住ctrl是否可以多选 }, check: { enable: true , chkStyle: 'ch ...
- Vue – 基础学习(4):事件修饰符
Vue – 基础学习(3):事件修饰符
- Mac OS中的”任务管理器“
在开发使用过程中,经常需要通过任务管理器来查看进程的一些情况以及杀掉一些进程,Mac中也有类似于Windows的”资源管理器“. 启动台->其他 找到”活动监视器“ 活动监视器即是”任务管理器“ ...
- windows下搭建vue+webpack的开发环境
1. 安装git其右键git bash here定位比cmd的命令行要准确,接下来的命令都是利用git bash here.2. 安装node.js一般利用vue创建项目是要搭配webpack项目构建 ...
- 华为企业级AS111-S,比较垃圾的地方
今天换了一个华为企业级AS111-S 路由器,比较垃圾的地方: 1. 网页管理界面是https,却用一个无效的证书,chrome直接不能访问,IE可以访问,但第一次登陆改密码的时候就出错了. 然后怎么 ...
- ORACLE隐藏参数查看及修改
查看隐藏参数 select x.ksppinm name, y.ksppstvl value, y.ksppstdf isdefault, decode(bitand(y.ksppstvf,7),1, ...
- 利用shell脚本将Oracle服务器中数据定时增量刷新到ftp服务器中
现有需求:将oracle数据库中的数据准实时同步至某ftp服务器中,以便前端应用能定时从ftp服务器目录中取增量数据 方法:将加工脚本写为存储过程,然后利用shell脚本执行该存储过程并将增量数据导出 ...
- VSCode在Ubuntu下快捷键和Windows下不一致的解决办法
Windows下切换前一次和后一次光标位置,用的快捷键是Alt+<-和Alt+->.很遗憾,Ubuntu下并不是这个快捷键.不清楚为什么VSCode不提供统一的快捷键,但对于我来说,我很想 ...