> 文章列表 > 如何使用evosuite为指定被测方法生成测试用例

如何使用evosuite为指定被测方法生成测试用例

如何使用evosuite为指定被测方法生成测试用例

目录

省流版本

准备工作

环境

evosuite获取

检验环境

参数解释

怎样表示被测方法

怎样指向被测类

其他参数

参考


省流版本

java -jar .\\target\\depd\\evosuite-1.1.0.jar 
-generateTests 
-Dtarget_method="isLenient()Z" 
-class com.google.gson.stream.JsonWriter 
-projectCP .\\target\\classes\\ 
-seed 330 
-Dnew_statistics=false 
-criterion branch

准备工作

环境

编译器:IDEA IntelliJ

使用evosuite的方式:command line (与maven使用方式、IDEA插件使用方式相对,后两种我也用过,终于未成功)

evosuite获取

版本:1.1.0

获取地址:https://github.com/EvoSuite/evosuite/releases/tag/v1.1.0

只要第一个

 如何安放:

可以在java项目的target目录下新建depd,然后将jar移动进去

检验环境

在项目的根目录(我这里是C:\\dataset\\d4j-spec5\\gson\\v-github\\gson-master\\gson>)下运行

 java -jar .\\target\\depd\\evosuite-1.1.0.jar

出现以下内容说明环境配好了,也可以顺便摸索一下参数

* EvoSuite 1.1.0
usage: EvoSuite-base_dir <arg>            Working directory in which tests and reportswill be placed-class <arg>               target class for test generation. A fullyqualifying needs to be provided, e.g.org.foo.SomeClass-continuous <arg>          Run Continuous Test Generation (CTG). Validvalues are: [EXECUTE, INFO, CLEAN]-criterion <arg>           target criterion for test generation. Candefine more than one criterion by using a ':'separated list-D <property=value>        use value for given property-evosuiteCP <arg>          classpath of EvoSuite jar file(s). This isneeded when EvoSuite is called in plugins likeEclipse/Maven-generateMOSuite           use many objective test generation (MOSA).-generateNumRandom <arg>   generate fixed number of random tests-generateRandom            use random test generation-generateSuite             use whole suite generation. This is thedefault behavior-generateSuiteUsingDSE     use Dynamic Symbolic Execution to generatetest suite-generateTests             use individual test generation (old approachfor reference purposes)-heapdump                  Create heap dump on client VM out of memoryerror-help                      print this message-inheritanceTree           Cache inheritance tree during setup-junit <arg>               junit prefix-libraryPath <arg>         java library path to native libraries of theproject under testclasses on the classpath with the givenpackage prefix will be used, i.e. all classesin the given package and sub-packages.-printStats                print class information (coverable goals)-projectCP <arg>           classpath of the project under test and allits dependencies-seed <arg>                seed for random number generator-setup <arg>               Create evosuite-files with property file-startedByCtg              Determine if current process was started by aCTG process-target <arg>              target classpath for test generation. Either ajar file or a folder where to find the .classfiles-writeDependencies <arg>   write the dependencies of a target class tofile

参数解释

怎样表示被测方法

回到标题,比如,我希望针对JsonWriter这个类中的 isLenient 生成测试用例

 public boolean isLenient() {return lenient;}

这个时候就需要用到参数

-Dtarget_method

如何赋值呢?

变化后的参数签名放到引号中

即isLenient()Z

变化规则参见

https://asm.ow2.io/asm4-guide.pdf

中的2.1.3节

怎样指向被测类

这里又有个坑,指向被测类,并不是JsonWriter.java,而是JsonWriter.class,后者在target文件夹下

因而用到参数

-projectCP .\\target\\classes\\ 

表示类的时候层次关系用点隔开

-class com.google.gson.stream.JsonWriter 

其他参数

使用单个测试生成

-generateTests 

设置随机种子,使得能够复现

-seed 330 

这个参数是为了应对一个报错

-Dnew_statistics=false 

选择生成的用例的标准,这里选择分支覆盖,话说我也试过weakmutation,结果就是卡死

-criterion branch

参考

https://github.com/EvoSuite/evosuite/issues/261