1. 概述
本文将带你快速掌握 TestNG 测试框架的核心功能。我们将重点讲解:
- 框架搭建
- 基础测试用例编写与配置
- 测试执行方式
- 测试报告生成
- 并发测试执行
2. 环境搭建
在 pom.xml
中添加 Maven 依赖即可快速集成:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
最新版本可在 Maven 仓库 查询。
Eclipse 用户可通过 Eclipse Marketplace 安装 TestNG 插件。
3. 编写测试用例
使用 TestNG 编写测试只需在方法上添加 @org.testng.annotations.Test
注解:
@Test
public void givenNumber_whenEven_thenTrue() {
assertTrue(number % 2 == 0);
}
4. 测试配置
实际测试中常需要初始化和清理操作。TestNG 提供多层级配置注解:
@BeforeClass
public void setup() {
number = 12;
}
@AfterClass
public void tearDown() {
number = 0;
}
✅ 核心配置注解:
@BeforeMethod
/@AfterMethod
:方法级别@BeforeClass
/@AfterClass
:类级别@BeforeGroup
/@AfterGroup
:分组级别@BeforeTest
/@AfterTest
:测试套件级别@BeforeSuite
/@AfterSuite
:全局套件级别
5. 测试执行
5.1 Maven 执行
直接运行 mvn test
会执行所有 @Test
注解方法(默认套件)。
5.2 XML 套件执行
通过 maven-surefire-plugin
指定 XML 配置文件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>
src\test\resources\test_suite.xml
</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
多 XML 文件配置示例:
<suiteXmlFiles>
<suiteXmlFile>
src/test/resources/parametrized_test.xml
</suiteXmlFile>
<suiteXmlFile>
src/test/resources/registration_test.xml
</suiteXmlFile>
</suiteXmlFiles>
5.3 独立执行
确保 TestNG 库在 classpath 中,执行命令:
java org.testng.TestNG test_suite.xml
6. 测试分组
6.1 套件分组
通过 XML 文件组织测试类:
<suite name="suite">
<test name="test suite">
<classes>
<class name="com.baeldung.RegistrationTest" />
<class name="com.baeldung.SignInTest" />
</classes>
</test>
</suite>
执行套件时,所有测试类方法都会运行。
6.2 方法分组
在 @Test
注解中指定 groups
参数:
@Test(groups = "regression")
public void givenNegativeNumber_sumLessthanZero_thenCorrect() {
int sum = numbers.stream().reduce(0, Integer::sum);
assertTrue(sum < 0);
}
XML 配置执行指定分组:
<test name="test groups">
<groups>
<run>
<include name="regression" />
</run>
</groups>
<classes>
<class
name="com.baeldung.SummationServiceTest" />
</classes>
</test>
7. 参数化测试
7.1 XML 参数化
使用 @Parameters
注解从 XML 获取参数:
@Test
@Parameters({"value", "isEven"})
public void
givenNumberFromXML_ifEvenCheckOK_thenCorrect(int value, boolean isEven) {
assertEquals(isEven, value % 2 == 0);
}
7.2 DataProvider 参数化
⚠️ 原文此处缺失示例,实际开发中推荐使用 @DataProvider
实现复杂数据驱动测试。
10. 并发测试执行
10.1 类与方法级并发
在 XML 中配置 parallel
属性:
<suite name="suite" parallel="classes" thread-count="2">
<test name="test suite">
<classes>
<class name="baeldung.com.RegistrationTest" />
<class name="baeldung.com.SignInTest" />
</classes>
</test>
</suite>
✅ 并发模式选项:
parallel="classes"
:类级并发parallel="tests"
:测试套件并发parallel="methods"
:方法级并发
10.2 方法多线程执行
通过注解实现方法级多线程测试:
public class MultiThreadedTests {
@Test(threadPoolSize = 5, invocationCount = 10, timeOut = 1000)
public void givenMethod_whenRunInThreads_thenCorrect() {
int count = Thread.activeCount();
assertTrue(count > 1);
}
}
参数说明:
threadPoolSize
:线程池大小invocationCount
:调用次数timeOut
:超时时间(毫秒)
11. 功能测试
TestNG 结合 Selenium 可实现 Web 应用功能测试,或配合 HttpClient 进行 Web 服务测试。
📚 扩展阅读:
12. 总结
本文快速介绍了 TestNG 的核心功能:
- 环境搭建与基础用法
- 测试配置与执行策略
- 分组测试与参数化
- 并发测试实现
- 功能测试集成
更多高级特性(如依赖测试、忽略测试等)可参考 JUnit vs TestNG 对比。
所有示例代码可在 GitHub 获取。