- 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 vs JUnit
- TestNG 有用资源
- TestNG - 快速指南
- TestNG - 有用资源
- TestNG - 讨论
TestNG - 基本注解 - BeforeGroups
@BeforeGroups 注解的方法只会在所有属于指定组的测试方法执行之前运行一次。
以下是@BeforeGroups注解支持的属性列表
| 属性 | 描述 |
|---|---|
alwaysRun |
对于 before 方法(beforeSuite、beforeTest、beforeTestClass 和 beforeTestMethod,但不包括 beforeGroups):如果设置为 true,则无论此配置方法属于哪些组,都会运行。 对于 after 方法(afterSuite、afterClass……):如果设置为 true,即使之前调用的一个或多个方法失败或被跳过,此配置方法也会运行。 |
dependsOnGroups |
此方法依赖的组列表。 |
dependsOnMethods |
此方法依赖的方法列表。 |
enabled |
此类/方法上的方法是否启用。 |
groups |
此类/方法所属的组列表。 |
inheritGroups |
如果为 true,则此方法将属于类级别 @Test 注解中指定的组。 |
onlyForGroups |
仅适用于 @BeforeMethod 和 @AfterMethod。如果指定,则只有当相应的测试方法属于列出的组之一时,才会调用此设置/拆卸方法。 |
创建类
创建一个要测试的 Java 类,例如,在/work/testng/src 中创建MessageUtil.java。
/*
* This class prints the given message on console.
*/
public class MessageUtil {
private String message;
//Constructor
//@param message to be printed
public MessageUtil(String message) {
this.message = message;
}
// prints the message
public String printMessage() {
System.out.println(message);
return message;
}
}
创建测试用例类
创建一个 Java 测试类,例如,在/work/testng/src 中创建TestAnnotationBeforeGroups.java。
向测试类添加测试方法 testMethod()。
向 testMethod() 方法添加注解 @Test,并将此方法添加到组testOne。
向测试类添加一个带有注解 @BeforeGroups 的 beforeGroups 方法,并通过添加注解@BeforeGroups("testOne")在testOne组之前执行它。
实现测试条件并检查 @BeforeGroups 注解的行为。
以下是TestAnnotationBeforeGroups.java的内容
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeGroups;
public class TestAnnotationBeforeGroups {
MessageUtil messageUtil = new MessageUtil("Test method");
@BeforeGroups("testOne")
public void beforeGroups(){
System.out.println("beforeGroups method executed before testOne group");
}
@Test(groups={"testOne"})
public void testMethod(){
Assert.assertEquals("Test method helooo", messageUtil.printMessage());
}
}
创建testng.xml
接下来,让我们在/work/testng/src中创建 testng.xml 文件来执行测试用例。此文件以 XML 格式捕获您的整个测试过程。此文件使您可以轻松地在单个文件中描述所有测试套件及其参数,您可以将其检入代码存储库或通过电子邮件发送给同事。它还可以轻松提取测试的子集或拆分多个运行时配置(例如,testngdatabase.xml 只运行测试您的数据库的测试)。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test">
<classes>
<class name="TestAnnotationBeforeGroups"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
使用 javac 编译测试用例。
/work/testng/src$ javac TestAnnotationBeforeGroups.java MessageUtil.java
现在,运行 testng.xml,它将运行在 <test> 标签中定义的测试用例。正如您所看到的,@BeforeGroups 在所有其他测试用例之前被调用。
/work/testng/src$ java org.testng.TestNG testng.xml
验证输出。
beforeGroups method executed before testOne group Test method =============================================== Suite Total tests run: 1, Passes: 0, Failures: 1, Skips: 0 ===============================================