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

相关文章

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

使用Java读取本地文件并转换为MultipartFile对象的方法

《使用Java读取本地文件并转换为MultipartFile对象的方法》在许多JavaWeb应用中,我们经常会遇到将本地文件上传至服务器或其他系统的需求,在这种场景下,MultipartFile对象非... 目录1. 基本需求2. 自定义 MultipartFile 类3. 实现代码4. 代码解析5. 自定

Python文本相似度计算的方法大全

《Python文本相似度计算的方法大全》文本相似度是指两个文本在内容、结构或语义上的相近程度,通常用0到1之间的数值表示,0表示完全不同,1表示完全相同,本文将深入解析多种文本相似度计算方法,帮助您选... 目录前言什么是文本相似度?1. Levenshtein 距离(编辑距离)核心公式实现示例2. Jac

C#高效实现Word文档内容查找与替换的6种方法

《C#高效实现Word文档内容查找与替换的6种方法》在日常文档处理工作中,尤其是面对大型Word文档时,手动查找、替换文本往往既耗时又容易出错,本文整理了C#查找与替换Word内容的6种方法,大家可以... 目录环境准备方法一:查找文本并替换为新文本方法二:使用正则表达式查找并替换文本方法三:将文本替换为图

SQL Server 查询数据库及数据文件大小的方法

《SQLServer查询数据库及数据文件大小的方法》文章介绍了查询数据库大小的SQL方法及存储过程实现,涵盖当前数据库、所有数据库的总大小及文件明细,本文结合实例代码给大家介绍的非常详细,感兴趣的... 目录1. 直接使用SQL1.1 查询当前数据库大小1.2 查询所有数据库的大小1.3 查询每个数据库的详

Java实现本地缓存的四种方法实现与对比

《Java实现本地缓存的四种方法实现与对比》本地缓存的优点就是速度非常快,没有网络消耗,本地缓存比如caffine,guavacache这些都是比较常用的,下面我们来看看这四种缓存的具体实现吧... 目录1、HashMap2、Guava Cache3、Caffeine4、Encache本地缓存比如 caff

Java 中编码与解码的具体实现方法

《Java中编码与解码的具体实现方法》在Java中,字符编码与解码是处理数据的重要组成部分,正确的编码和解码可以确保字符数据在存储、传输、读取时不会出现乱码,本文将详细介绍Java中字符编码与解码的... 目录Java 中编码与解码的实现详解1. 什么是字符编码与解码?1.1 字符编码(Encoding)1

Python Flask实现定时任务的不同方法详解

《PythonFlask实现定时任务的不同方法详解》在Flask中实现定时任务,最常用的方法是使用APScheduler库,本文将提供一个完整的解决方案,有需要的小伙伴可以跟随小编一起学习一下... 目录完js整实现方案代码解释1. 依赖安装2. 核心组件3. 任务类型4. 任务管理5. 持久化存储生产环境

Python批量替换多个Word文档的多个关键字的方法

《Python批量替换多个Word文档的多个关键字的方法》有时,我们手头上有多个Excel或者Word文件,但是领导突然要求对某几个术语进行批量的修改,你是不是有要崩溃的感觉,所以本文给大家介绍了Py... 目录工具准备先梳理一下思路神奇代码来啦!代码详解激动人心的测试结语嘿,各位小伙伴们,大家好!有没有想

Python如何调用另一个类的方法和属性

《Python如何调用另一个类的方法和属性》在Python面向对象编程中,类与类之间的交互是非常常见的场景,本文将详细介绍在Python中一个类如何调用另一个类的方法和属性,大家可以根据需要进行选择... 目录一、前言二、基本调用方式通过实例化调用通过类继承调用三、高级调用方式通过组合方式调用通过类方法/静