TestNG ----注解使用之Before/After

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方法都为测试方法



创建一个demo 结构如下:

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");

    }

     

     

}

 

当前接口自动化测试场景:在用例全部执行完成后,清除数据

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值