TestNG中的方法拦截器(重新排序测试方法)

2024-04-16 22:04

本文主要是介绍TestNG中的方法拦截器(重新排序测试方法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在本文中,我们将讨论如何在TestNG中使用方法拦截器。TestNG按照方法名称的字母顺序运行我们的测试用例。如果TestNG已经决定了调用测试方法的顺序,那么我们可以将其分为两组:

  • 方法以特定的顺序运行因此,依赖方法将在依赖方法之前运行。
  • 该方法的运行顺序不特别为了给予更多的控制权,TestNG为我们提供了方法拦截器。
public interface IMethodInterceptor extends ITestNGListener {List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context);
}

“intercept”方法是做什么的?它将在其参数中接受可以以任何顺序运行的方法列表,并返回类似的方法列表。
“intercept”方法返回什么?它将返回在其参数中传递的方法的类似列表,并且返回的列表可以是-

  • 我们在参数中接收到相同的列表,但顺序不同。
  • IMethodInstance方法的较小列表。
  • IMethodInstance方法的列表比参数中接收的方法的列表大。

让我们先拿一个测试类的例子,在这个类中我们将尝试使用方法拦截器来改变测试方法的顺序。

CodekruTest.java

package Test;import org.testng.Assert;
import org.testng.annotations.Test;public class CodekruTest {@Testpublic void gammaMethod() {System.out.println("Executing gammaMethod in CodekruTest class");Assert.assertTrue(true);}@Testpublic void alphaMethod() {System.out.println("Executing alphaMethod in CodekruTest class");Assert.assertTrue(true);}@Testpublic void betaMethod() {System.out.println("Executing betaMethod in CodekruTest class");Assert.assertTrue(true);}@Testpublic void deltaMethod() {System.out.println("Executing deltaMethod in CodekruTest class");Assert.assertTrue(true);}}

xml文件

<suite name="codekru"><test name="codekruTest"><classes><class name="Test.CodekruTest"></class></classes></test>
</suite>

运行结果:

Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class

 

我们应该怎么做才能让一个特定的方法(比如说,本例中的deltaMethod)总是先运行?

因为我们想先运行一个特定的方法。我们可以通过多种方式来实现-

  • 通过为该方法分配优先级,使其始终首先运行。这很容易做到,但是这个类可能包含更多的测试方法,并且其他一些测试方法可能也需要一些优先级。如果我们将优先级作为一个非常负的值(比如-12345)传递,那么使用优先级执行方法是一个很好的方法。此值
  • 第二种方法是使用方法拦截器。方法拦截器提供了一个很好的接口来按照我们想要的顺序运行我们的测试。

下面是实现IMethodInterceptor接口并首先执行deltaMethod的类。

package Test;import java.util.ArrayList;
import java.util.List;import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;public class CodekruMethodInterceptor implements IMethodInterceptor {public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {List<IMethodInstance> result = new ArrayList<IMethodInstance>();for (IMethodInstance m : methods) {if (m.getMethod().getMethodName().equalsIgnoreCase("deltaMethod")) {result.add(0, m);} else {result.add(m);}}return result;}}

现在,我们必须将上面的类作为侦听器添加到XML文件中,如下所示。

<suite name="codekru"><listeners><listener class-name="Test.CodekruMethodInterceptor" /></listeners><test name="codekruTest"><classes><class name="Test.CodekruTest"></class></classes></test>
</suite>

运行XML结果:

Executing deltaMethod in CodekruTest classExecuting alphaMethod in CodekruTest classExecuting betaMethod in CodekruTest class Executing gammaMethod in CodekruTest class

让我们尝试在以特定顺序运行的方法上使用它(方法依赖于其他方法)。下面是我们的CodekruTest类,但我们已经修改了它,以按照特定的顺序运行方法。

package Test;import org.testng.Assert;
import org.testng.annotations.Test;public class CodekruTest {@Test(dependsOnMethods = {"betaMethod"})public void gammaMethod() {System.out.println("Executing gammaMethod in CodekruTest class");Assert.assertTrue(true);}@Testpublic void alphaMethod() {System.out.println("Executing alphaMethod in CodekruTest class");Assert.assertTrue(true);}@Test(dependsOnMethods = {"alphaMethod"})public void betaMethod() {System.out.println("Executing betaMethod in CodekruTest class");Assert.assertTrue(true);}@Test(dependsOnMethods = {"gammaMethod"})public void deltaMethod() {System.out.println("Executing deltaMethod in CodekruTest class");Assert.assertTrue(true);}}

现在,再次使用相同的XML文件。

<suite name="codekru"><listeners><listener class-name="Test.CodekruMethodInterceptor" /></listeners><test name="codekruTest"><classes><class name="Test.CodekruTest"></class></classes></test>
</suite>

运行XML文件后的输出-

Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class

我们可以看到,deltaMethod是在最后执行的,因为它依赖于另一个方法,而另一个方法又依赖于另一个方法。因此,在执行案件时遵循了这一命令。

如果我们想先运行一个特定的组呢?

我们将从TestNG文档中挑选代码,但在撰写本文时,TestNG文档中的代码中有一个小问题。所以,我们将在这里编写一个功能齐全的代码。

让我们再次使用CodekruTest类

package Test;import org.testng.Assert;
import org.testng.annotations.Test;public class CodekruTest {@Test(groups = { "func" })public void gammaMethod() {System.out.println("Executing gammaMethod in CodekruTest class");Assert.assertTrue(true);}@Testpublic void alphaMethod() {System.out.println("Executing alphaMethod in CodekruTest class");Assert.assertTrue(true);}@Test(groups = { "func" })public void betaMethod() {System.out.println("Executing betaMethod in CodekruTest class");Assert.assertTrue(true);}@Testpublic void deltaMethod() {System.out.println("Executing deltaMethod in CodekruTest class");Assert.assertTrue(true);}}

在这里,我们希望首先运行属于“func”组的方法,其余的方法应该在之后执行。下面是我们的方法拦截器代码。

package Test;import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
import org.testng.annotations.Test;public class CodekruMethodInterceptor implements IMethodInterceptor {public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {List<IMethodInstance> result = new ArrayList<IMethodInstance>();for (IMethodInstance m : methods) {Test test = m.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class);Set<String> groups = new TreeSet<String>();for (String group : test.groups()) {groups.add(group);}// if group is "func", then put the method// at the first position in the listif (groups.contains("func")) {result.add(0, m);} else {result.add(m);}}return result;}}

下面是用于执行CodekruTest类的XML,其中属于func的测试方法将首先执行。

<suite name="codekru"><listeners><listener class-name="Test.CodekruMethodInterceptor" /></listeners><test name="codekruTest"><classes><class name="Test.CodekruTest"></class></classes></test>
</suite>

运行上述XML文件后的输出-

Executing gammaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing alphaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class

这里我们可以看到gammaMethod和betaMethod首先被执行,因为它们属于“func”组。

这篇关于TestNG中的方法拦截器(重新排序测试方法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/910011

相关文章

mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)的解决方法

《mysql出现ERROR2003(HY000):Can‘tconnecttoMySQLserveron‘localhost‘(10061)的解决方法》本文主要介绍了mysql出现... 目录前言:第一步:第二步:第三步:总结:前言:当你想通过命令窗口想打开mysql时候发现提http://www.cpp

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

CentOS 7部署主域名服务器 DNS的方法

《CentOS7部署主域名服务器DNS的方法》文章详细介绍了在CentOS7上部署主域名服务器DNS的步骤,包括安装BIND服务、配置DNS服务、添加域名区域、创建区域文件、配置反向解析、检查配置... 目录1. 安装 BIND 服务和工具2.  配置 BIND 服务3 . 添加你的域名区域配置4.创建区域

mss32.dll文件丢失怎么办? 电脑提示mss32.dll丢失的多种修复方法

《mss32.dll文件丢失怎么办?电脑提示mss32.dll丢失的多种修复方法》最近,很多电脑用户可能遇到了mss32.dll文件丢失的问题,导致一些应用程序无法正常启动,那么,如何修复这个问题呢... 在电脑常年累月的使用过程中,偶尔会遇到一些问题令人头疼。像是某个程序尝试运行时,系统突然弹出一个错误提

电脑提示找不到openal32.dll文件怎么办? openal32.dll丢失完美修复方法

《电脑提示找不到openal32.dll文件怎么办?openal32.dll丢失完美修复方法》openal32.dll是一种重要的系统文件,当它丢失时,会给我们的电脑带来很大的困扰,很多人都曾经遇到... 在使用电脑过程中,我们常常会遇到一些.dll文件丢失的问题,而openal32.dll的丢失是其中比较

python中字符串拼接的几种方法及优缺点对比详解

《python中字符串拼接的几种方法及优缺点对比详解》在Python中,字符串拼接是常见的操作,Python提供了多种方法来拼接字符串,每种方法有其优缺点和适用场景,以下是几种常见的字符串拼接方法,需... 目录1. 使用 + 运算符示例:优缺点:2. 使用&nbsjsp;join() 方法示例:优缺点:3

Mysql中深分页的五种常用方法整理

《Mysql中深分页的五种常用方法整理》在数据量非常大的情况下,深分页查询则变得很常见,这篇文章为大家整理了5个常用的方法,文中的示例代码讲解详细,大家可以根据自己的需求进行选择... 目录方案一:延迟关联 (Deferred Join)方案二:有序唯一键分页 (Cursor-based Paginatio

SpringBoot项目启动报错"找不到或无法加载主类"的解决方法

《SpringBoot项目启动报错找不到或无法加载主类的解决方法》在使用IntelliJIDEA开发基于SpringBoot框架的Java程序时,可能会出现找不到或无法加载主类com.example.... 目录一、问题描述二、排查过程三、解决方案一、问题描述在使用 IntelliJ IDEA 开发基于

MySQL中慢SQL优化方法的完整指南

《MySQL中慢SQL优化方法的完整指南》当数据库响应时间超过500ms时,系统将面临三大灾难链式反应,所以本文将为大家介绍一下MySQL中慢SQL优化的常用方法,有需要的小伙伴可以了解下... 目录一、慢SQL的致命影响二、精准定位问题SQL1. 启用慢查询日志2. 诊断黄金三件套三、六大核心优化方案方案