TestNG教程

TestNG @BeforeGroups

TestNG @BeforeGroups Annotation

TestNG 允许测试人员通过使用 @Test 注释中的属性"group"将多个测试用例创建到一个组中。我们可以说 TestNG 组允许您在同一组中添加类似的功能。例如,student_id、student_name、student_address 是学生的详细信息,所有这些详细信息都添加到同一个组中,即"学生详细信息"。
@BeforeGroups: @BeforeGroups 注解的方法只会在属于指定组的所有测试方法执行完毕前运行一次。
让我们通过一个例子来理解@BeforeGroups 注解。
第 1 步: 打开 Eclipse。
第 2 步: 我们创建一个简单的 Java 项目。
Class1.java
package com.lidihuo;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
public class Class1 
{
@BeforeGroups("IT Department")
public void before_it()
{
System.out.println("this method will be executed before the execution of IT Department group");
}
@Test
public void testcase1()
{
System.out.println("HR");
}
 @Test(groups= {"IT Department"})
public void testcase2()
{
System.out.println("Software Developer");
}
@Test(groups= {"IT Department"})
public void testcase3()
{
System.out.println("QA Analyst");
}
}
    
上面我们创建了java项目,在其中定义了@BeforeGroups注解方法,在@BeforeGroups中,传入了"IT部门",表示@BeforeGroups注解方法,即 before_it() 将在执行属于"IT 部门"组的所有测试方法之前调用。
第 3 步: 现在,我们创建一个 testng.xml 文件来配置上述类。
testng.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="test_suite">
<test name="IT Department">
<classes>
<class name="com.lidihuo.Class1"/>
</classes>
</test> <!--Test-->
</suite> <!--Suite-->
    
第 4 步: 运行 testng.xml 文件。右键单击 testng.xml 文件,将光标向下移动到 Run As,然后单击 1 TestNG Suite。
TestNG @BeforeGroups Annotation
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4