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文件中增加如下代码

 

  1.  
    package test.beforafter;
  2.  
    import org.testng.annotations.AfterClass;
  3.  
    import org.testng.annotations.AfterTest;
  4.  
    import org.testng.annotations.AfterGroups;
  5.  
    import org.testng.annotations.AfterSuite;
  6.  
    import org.testng.annotations.AfterMethod;
  7.  
    import org.testng.annotations.BeforeClass;
  8.  
    import org.testng.annotations.BeforeTest;
  9.  
    import org.testng.annotations.BeforeGroups;
  10.  
    import org.testng.annotations.BeforeSuite;
  11.  
    import org.testng.annotations.BeforeMethod;
  12.  
    import org.testng.annotations.Test;
  13.  
     
  14.  
     
  15.  
    public class TestClass {
  16.  
     
  17.  
     
  18.  
    @BeforeSuite
  19.  
    public void beforeSuite(){
  20.  
    System.out.println("Before Suite Method");
  21.  
    }
  22.  
     
  23.  
    @AfterSuite
  24.  
    public void afterSuite(){
  25.  
    System.out.println("After Suite Method");
  26.  
    }
  27.  
     
  28.  
    @BeforeTest
  29.  
    public void beforeTest(){
  30.  
    System.out.println("Before Test Method");
  31.  
    }
  32.  
     
  33.  
    @AfterTest
  34.  
    public void afterTest(){
  35.  
    System.out.println("After Test Method");
  36.  
    }
  37.  
     
  38.  
    @BeforeClass
  39.  
    public void beforeClass(){
  40.  
    System.out.println("Before Class Method");
  41.  
    }
  42.  
     
  43.  
    @AfterClass
  44.  
    public void afterClass(){
  45.  
    System.out.println("After Class Method");
  46.  
    }
  47.  
     
  48.  
    @BeforeGroups(groups={"testOne"})
  49.  
    public void beforeGroupOne(){
  50.  
    System.out.println("Before Group testOne Method");
  51.  
    }
  52.  
     
  53.  
    @AfterGroups(groups={"testOne"})
  54.  
    public void afterGroupOne(){
  55.  
    System.out.println("After group testOne Method");
  56.  
    }
  57.  
     
  58.  
    @BeforeGroups(groups={"testTwo"})
  59.  
    public void beforeGroupTwo(){
  60.  
    System.out.println("Before Group testTwo Method");
  61.  
    }
  62.  
     
  63.  
    @AfterGroups(groups={"testTwo"})
  64.  
    public void afterGroupTwo(){
  65.  
    System.out.println("After Group testTwo Method");
  66.  
    }
  67.  
    @BeforeMethod
  68.  
    public void beforeMethod(){
  69.  
    System.out.println("Before Method");
  70.  
    }
  71.  
     
  72.  
    @AfterMethod
  73.  
    public void afterMethod(){
  74.  
    System.out.println("After Method");
  75.  
    }
  76.  
     
  77.  
    @Test(groups={"testOne"})
  78.  
    public void testOne(){
  79.  
    System.out.print("Test One Method");
  80.  
    }
  81.  
     
  82.  
    @Test(groups={"testTwo"})
  83.  
    public void testTwo(){
  84.  
     
  85.  
    System.out.print("Test Two Method");
  86.  
    }
  87.  
     
  88.  
    @Test
  89.  
    public void testThree(){
  90.  
     
  91.  
    System.out.println("Test Third Method");
  92.  
    }
  93.  
     
  94.  
     
  95.  
    }


alltestng.xml文件中增加如下配置

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!-- 只执行某个包 -->
  3.  
    <suite name="Include Package Suite" verbose="1">
  4.  
    <test name="Include Package Test">
  5.  
    <packages>
  6.  
    <package name="test.*">
  7.  
    <include name="test.beforafter" />
  8.  
    </package>
  9.  
    </packages>
  10.  
    </test>
  11.  
    </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

posted @ 2019-11-21 16:39  #甜甜8023  阅读(748)  评论(0编辑  收藏  举报