- TestNG 教程
- TestNG - 主页
- 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中创建名为TestngAnnotation.java 的 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() 之间执行。
广告