测者的测试技术手册:智能化测试框架EvoSuite的一个坑以及填坑方法

栏目: 编程工具 · 发布时间: 7年前

内容简介:最近在不断地学习和探索EvoSuite框架的时候,在生产JUnit单元测试框架后,出现如下问题:所有的EvoSuite生成的单元测试class都继承自一个同名的scaffolding的类,在对应的脚手架后缀名的类中的@BeforeClass部分,有一个参数:上述报错就是因为这一项的设置造成的。那么该org.evosuite.runtime.RuntimeSettings.maxNumberOfIterationPerLoop是干什么的呢?这里我们就要进入EvoSuite的源代码里面看看了。

问题

最近在不断地学习和探索EvoSuite框架的时候,在生产JUnit单元测试框架后,出现如下问题:

Exception:
Caused by: org.evosuite.runtime.TooManyResourcesException: Loop has been executed more times than the allowed 10000
at org.evosuite.runtime.LoopCounter.checkLoop(LoopCounter.java:115)
at org.apache.xerces.impl.io.UTF8Reader.read(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.skipSpaces(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
at org.apache.poi.util.DocumentHelper.readDocument(DocumentHelper.java:137)
at org.apache.poi.POIXMLTypeLoader.parse(POIXMLTypeLoader.java:115)
at org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument$Factory.parse(Unknown Source)
at org.apache.poi.xssf.model.StylesTable.readFrom(StylesTable.java:203)
at org.apache.poi.xssf.model.StylesTable.(StylesTable.java:146)

分析和修订

所有的EvoSuite生成的单元测试class都继承自一个同名的scaffolding的类,在对应的脚手架后缀名的类中的@BeforeClass部分,有一个参数:

org.evosuite.runtime.RuntimeSettings.maxNumberOfIterationPerLoop=10000;

上述报错就是因为这一项的设置造成的。那么该org.evosuite.runtime.RuntimeSettings.maxNumberOfIterationPerLoop是干什么的呢?这里我们就要进入EvoSuite的源代码里面看看了。

经过源代码分析,在源代码中:

evosuite/runtime/src/main/java/org/evosuite/runtime/LoopCounter.java

发现其110行到123行部分能够抛出对应的异常,依据作者注释,该类就是为了避免无限循环的出现而专门做的一个循环计数的类。通过源代码的异常抛出位置可以看出就是一位代码执行的循环次数超过了10000而抛出的异常,这么大次数的循环有可能是mock的数据导致的,也有可能是内部脚本逻辑异常导致的。

解决该问题

通过对应github项目的查找发现这不是一个fixed的issue。但是可以通过设置该参数避免该异常出现。 通过maxNumberOfIterationPerLoop出现的条件部分的判断: evosuite/runtime/src/main/java/org/evosuite/runtime/LoopCounter.java的96到98行,如下:

 if(RuntimeSettings.maxNumberOfIterationsPerLoop < 0){            return; //do nothing, no check
        }

和/Users/chancriss/Desktop/WorkSpace/JavaSpace/github/evosuite/runtime/src/main/java/org/evosuite/runtime/instrumentation/RuntimeInstrumentation.java下的144行到146行,如下:

if (RuntimeSettings.maxNumberOfIterationsPerLoop >= 0) {
			cv = new LoopCounterClassAdapter(cv);
		}

可知,要避免这类问题的出现,并不是在对应的JUnit脚本的父类中扩大org.evosuite.runtime.RuntimeSettings.maxNumberOfIterationPerLoop的值,而是将其设置成为一个小于0的值。

由于测者并没有完全掌握该框架,因此这种的修改到底会带来什么后果还没有进一步的研究。

关注测者,关注测试


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Chinese Authoritarianism in the Information Age

Chinese Authoritarianism in the Information Age

Routledge / 2018-2-13 / GBP 115.00

This book examines information and public opinion control by the authoritarian state in response to popular access to information and upgraded political communication channels among the citizens in co......一起来看看 《Chinese Authoritarianism in the Information Age》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具