个性化阅读
专注于IT技术分析

TestNG @BeforeGroups注解用法示例

TestNG允许测试人员通过使用@Test批注中的属性’group’将多个测试用例创建到一个组中。我们可以说TestNG组允许你在同一组中添加相似的功能。例如, student_id, student_name, student_address是学生的详细信息, 并且所有这些详细信息被添加在同一组中, 即”学生详细信息”。

@BeforeGroups:@BeforeGroups批注的方法将在执行属于指定组的所有测试方法之前仅运行一次。

让我们通过示例了解@BeforeGroups批注。

步骤1:打开Eclipse。

步骤2:我们创建一个简单的Java项目。

Class1.java

package com.srcmini;
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 Department”, 这意味着@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.srcmini.Class1"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

步骤4:运行testng.xml文件。右键单击testng.xml文件, 将光标向下移动到Run As, 然后单击1 TestNG Suite。

TestNG @BeforeGroups注解

赞(0)
未经允许不得转载:srcmini » TestNG @BeforeGroups注解用法示例

评论 抢沙发

评论前必须登录!