下面是 TestNG 测试 API 方法的执行过程和示例。
创建java类文件名 TestngAnnotation.java 在 /work/testng/src 测试注释。
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
public class TestngAnnotation {// test case 1@Testpublic void testCase1() {System.out.println("in test case 1");}// test case 2@Testpublic void testCase2() {System.out.println("in test case 2");}@BeforeMethodpublic void beforeMethod() {System.out.println("in beforeMethod");}@AfterMethodpublic void afterMethod() {System.out.println("in afterMethod");}@BeforeClasspublic void beforeClass() {System.out.println("in beforeClass");}@AfterClasspublic void afterClass() {System.out.println("in afterClass");}@BeforeTestpublic void beforeTest() {System.out.println("in beforeTest");}@AfterTestpublic void afterTest() {System.out.println("in afterTest");}@BeforeSuitepublic void beforeSuite() {System.out.println("in beforeSuite");}@AfterSuitepublic void afterSuite() {System.out.println("in afterSuite");}
}
验证输出
in beforeSuite
in beforeTest
in beforeClass
in beforeMethod
in test case 1
in afterMethod
in beforeMethod
in test case 2
in afterMethod
in afterClass
in afterTestin afterSuite
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
基于上述输出,执行程序如下 -