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 ...
随机推荐
- python课程单元三编程题讲解(上)
目录 1.快乐的数字 2.凯撒密码I 3.凯撒密码II 4.括号配对检测 A @ 下面向大家介绍一下我在学习python课程的一些题目的解法,如果大家有什么更好的解法请私信我.这里只显示题目与 ...
- 一文读懂Java线程状态转换
前言 本文描述Java线程线程状态及状态转换,不会涉及过多理论,主要以代码示例说明线程状态如何转换. 基础知识 1. 线程状态 Thread源码中的状态说明: 线程可以有6种状态: New(新建) R ...
- Python基础15
P75. 闭包,需再理解. 装饰器,语法糖
- Robotframework ride ,运行后提示, [WinError 2] 系统找不到指定的文件。
运行后提示, [WinError 2] 系统找不到指定的文件. command: pybot.bat --argumentfile C:\Users\123\AppData\Local\Temp\RI ...
- i春秋——“百度杯”CTF比赛 十月场——Login
根据页面源码提示的 test1 test1 登录 刷新此页面并抓包,有个show=0值得关注 在发送的包的header中加一句show:1,即可得到member.php的源码 <?php inc ...
- javascript:void(0); 和 href = "#"
在做页面时,如果想做一个链接点击后不做任何事情,或者响应点击而完成其他事情,可以设置其属性 href = "#", 但是,这样会有一个问题,就是当页面有滚动条时,点击后会返回到页面 ...
- cpio建立、还原备份档
1. 简介 加入.解开cpio或tar备份档内的文件 与tar相似,将文件归档到硬盘或磁带等存储设备中 2. tar比较 在所处理的文件类型方面,它比tar更全面,但也更复杂 cpio比tar更为可靠 ...
- 七月伊始 LeetCode算法总结
七月伊始 早上买了LeetCode的课程,解锁了付费题目,付费倒逼学习: 意识到这么久学习的东西,都是写在自己的笔记, 如今希望自己能够用自己拙笔记录这个学习和总结的过程. 队列的学习 设计循环队列 ...
- mysql中的where和having的区别
下面以一个例子来具体的讲解: 1. where和having都可以使用的场景 1)select addtime,name from dw_users where addtime> 1500000 ...
- C#8.0接口默认实现特性
文章:[译]C#8.0中一个使接口更加灵活的新特性-默认接口实现 原文示例代码: public interface IBook { void AddBook(string bookName, stri ...