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不同注释在何时执行:
/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
===============================================