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

相关文章

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

在Golang中实现定时任务的几种高效方法

《在Golang中实现定时任务的几种高效方法》本文将详细介绍在Golang中实现定时任务的几种高效方法,包括time包中的Ticker和Timer、第三方库cron的使用,以及基于channel和go... 目录背景介绍目的和范围预期读者文档结构概述术语表核心概念与联系故事引入核心概念解释核心概念之间的关系