- 测试NG教程
 - 测试NG - 主页
 - TestNG - 概述
 - TestNG - 环境
 - TestNG - 编写测试
 - TestNG - 基本注释
 - TestNG - 执行过程
 - TestNG - 执行测试
 - TestNG - 套件测试
 - TestNG - 忽略测试
 - TestNG - 小组测试
 - TestNG - 异常测试
 - TestNG - 依赖性测试
 - TestNG - 参数化测试
 - TestNG - 运行 JUnit 测试
 - TestNG - 测试结果
 - TestNG - 注释变压器
 - TestNG - 断言
 - TestNG - 并行执行
 - TestNG - 带 ANT 的插头
 - TestNG - 使用 Eclipse 进行插件
 - TestNG - TestNG - 与 JUnit
 
- TestNG 有用资源
 - TestNG - 快速指南
 - TestNG - 有用的资源
 - TestNG - 讨论
 
TestNG - 执行过程
本章解释了TestNG中方法的执行过程。它解释了调用方法的顺序。以下是 TestNG 测试 API 方法的执行过程和示例。
在/work/testng/src中创建一个java类文件名TestngAnnotation.java来测试注释。
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
   @Test
   public void testCase1() {
      System.out.println("in test case 1");
   }
   // test case 2
   @Test
   public void testCase2() {
      System.out.println("in test case 2");
   }
   @BeforeMethod
   public void beforeMethod() {
      System.out.println("in beforeMethod");
   }
   @AfterMethod
   public void afterMethod() {
      System.out.println("in afterMethod");
   }
   @BeforeClass
   public void beforeClass() {
      System.out.println("in beforeClass");
   }
   @AfterClass
   public void afterClass() {
      System.out.println("in afterClass");
   }
   @BeforeTest
   public void beforeTest() {
      System.out.println("in beforeTest");
   }
   @AfterTest
   public void afterTest() {
      System.out.println("in afterTest");
   }
   @BeforeSuite
   public void beforeSuite() {
      System.out.println("in beforeSuite");
   }
   @AfterSuite
   public void afterSuite() {
      System.out.println("in afterSuite");
   }
}
接下来,我们在/work/testng/src中创建文件testng.xml来执行注释。
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "TestngAnnotation"/>
      </classes>
   </test>
</suite>
使用 javac 编译测试用例类。
/work/testng/src$ javac TestngAnnotation.java
现在,运行 testng.xml,它将运行提供的测试用例类中定义的测试用例。
/work/testng/src$ java org.testng.TestNG testng.xml
验证输出。
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 afterTest in afterSuite =============================================== Suite Total tests run: 2, Failures: 0, Skips: 0 ===============================================
根据以上输出,执行过程如下 -
首先,beforeSuite()方法只执行一次。
最后,afterSuite() 方法仅执行一次。
即使是 beforeTest()、beforeClass()、afterClass() 和 afterTest() 方法也只执行一次。
beforeMethod() 方法在每个测试用例执行之前执行。
afterMethod() 方法针对每个测试用例执行,但在执行测试用例之后执行。
在 beforeMethod() 和 afterMethod() 之间,每个测试用例都会执行。