您的位置:首页 > 产品设计 > UI/UE

告诉你TestNG annotations注释都是什么时候运行的

2016-04-30 10:19 609 查看
package com.testng;

import org.testng.annotations.AfterClass;

import org.testng.annotations.AfterGroups;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterSuite;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.BeforeGroups;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.BeforeSuite;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

public class testngannotationsdemo {

    @Test(groups={"A组"})

    public void Case1(){

        System.out.println("Test:Case1被执行了!!");

    }

    @Test(groups={"A组"})

    public void Case2(){

        System.out.println("Test:Case2被执行了!!");

    }

    @Test

    public void Case3(){

        System.out.println("Test:Case3被执行了!!");

    }

    

    @BeforeMethod

    public void beforemethod(){

        System.out.println("BeforeMethod:在每个测试(@Test)运行之前执行");

    }

    @AfterMethod

    public void aftermethod(){

        System.out.println("AfterMethod:在每个测试(@Test)结束后执行");

    }

    @BeforeClass

    public void BeforeClass(){

        System.out.println("BeforeClass:在执行第一个测试前执行");

    }

    @AfterClass

    public void AfterClass(){

        System.out.println("AfterClass:在最后一个测试执行结束后执行");

    }

    @BeforeTest

    public void beforetest(){

        System.out.println("BeforeTest:在测试类中的测试开始执行前执行");

    }

    @AfterTest

    public void aftertest(){

        System.out.println("AfterTest:在测试类中的测试结束后执行");

    }

    @BeforeSuite

    public void beforesuite(){

        System.out.println("BeforeSuite:在执行测试集合前执行");

    }

    @AfterSuite

    public void aftersuite(){

        System.out.println("AferSuite:在执行测试集合前执行");

    }

    @BeforeGroups(groups={"A组"})

    public void beforegroups(){

        System.out.println("BeforeGroups:在分组(A组)用例任意一个用例运行前执行");

    }

    @AfterGroups(groups={"A组"})

    public void aftergroups(){

        System.out.println("AfterGroups:在分组(A组)所有用例运行结束后执行");

    }

    

}

如下执行结果可分析出 TestNG不同注释在何时执行:

[TestNG] Running:

  /private/var/folders/c1/wz0lthxd3n9693l0p1t7yw0h0000gn/T/testng-eclipse-1188532939/testng-customsuite.xml

BeforeSuite:在执行测试集合前执行

BeforeTest:在测试类中的测试开始执行前执行

BeforeClass:在执行第一个测试前执行

BeforeGroups:在分组(A组)用例任意一个用例运行前执行

BeforeMethod:在每个测试(@Test)运行之前执行

Test:Case1被执行了!!

AfterMethod:在每个测试(@Test)结束后执行

BeforeMethod:在每个测试(@Test)运行之前执行

Test:Case2被执行了!!

AfterMethod:在每个测试(@Test)结束后执行

AfterGroups:在分组(A组)所有用例运行结束后执行

BeforeMethod:在每个测试(@Test)运行之前执行

Test:Case3被执行了!!

AfterMethod:在每个测试(@Test)结束后执行

AfterClass:在最后一个测试执行结束后执行

AfterTest:在测试类中的测试结束后执行

PASSED: Case1

PASSED: Case2

PASSED: Case3

===============================================

    Default test

    Tests run: 3, Failures: 0, Skips: 0

===============================================

AferSuite:在执行测试集合前执行

===============================================

Default suite

Total tests run: 3, Failures: 0, Skips: 0

===============================================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息