Java单测代码生成工具之Evosuite

EvoSuite 是由 Sheffield 等大学联合开发的一种开源工具,用于自动生成测试用例集,生成的测试用例均符合 Junit的标准,可直接在 Junit 中运行。
通过使用此自动测试工具能够在保证代码覆盖率的前提下极大地提高测试人员的开发效率。但是只能辅助测试,并不能完全取代人工,测试用例的正确与否还需人工判断。
官网地址:https://www.evosuite.org/contact/
github地址:https://github.com/EvoSuite/evosuite

一 核心功能:
对于指定类,生成 Junit4 类型的单测代码。------Generation of JUnit 4 tests for the selected classes
根据不同覆盖指标调整生成的用例,例如 行覆盖,分支覆盖,输出覆盖等。------Optimization of different coverage criteria, like lines, branches, outputs and mutation testing
单测用例最小化,只有对覆盖率有贡献的单测用例才会被保留。------Tests are minimized: only the ones contributing to achieve coverage are retained
生成的单测用例中包含 Junit 断言。------Generation of JUnit asserts to capture the current behavior of the tested classes
测试运行在 sandbox 中。------Tests run in a sandbox to prevent potentially dangerous operations
虚拟文件系统。------Virtual file system
虚拟网络。------Virtual network
二、使用
官方提供了包括命令行工具、eclipse 插件、idea 插件、maven 插件 在内的数种运行方式。
参考官方的 http://www.evosuite.org/documentation/tutorial-part-2/

2.1 命令行工具
命令行进入到 项目根目录/target/classes/ 目录下,输入命令:

java -jar evosuite-1.0.6.jar -class ClassName -projectCP targetPath/。

参数说明:
-class :执行的对象
-projectCP:设置测试生成的类路径
-help:要查看可用的命令行选项
-criterion :测试的标准参数有(line、branch、cbranch、mutation 、exception等)
-Dminimize=false //它会删除所有不满足覆盖率目标所需的语句
-Dassertion_strategy=all //使用大量断言生成长测试
更过参数请参考开发文档:http://www.evosuite.org/documentation/tutorial-part-1/

2.2 maven 插件
调整 junit 版本(Junit4.12以上),并增加 evosuite 运行依赖

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.evosuite</groupId>
    <artifactId>evosuite-standalone-runtime</artifactId>
    <version>1.0.6</version>
    <scope>test</scope>
  </dependency>
</dependencies>
添加 evosuite 插件
<plugin>
   <groupId>org.evosuite.plugins</groupId>
   <artifactId>evosuite-maven-plugin</artifactId>
   <version>1.0.6</version>
</plugin>
有时候,我们会同时执行两类脚本,一类是RD手写的代码,一类是EvoSuite自动生成的,进入同时测试并不会出现什么大问题,但是也会对测试结果有片面的影响,因此需要只能EvoSuite仅对其生成的脚本起作用,需要在pom中加入如下插件。
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.20</version>
   <configuration>
     <properties>
       <property>
          <name>listener</name>
          <value>org.evosuite.runtime.InitializingListener</value>
      </property>
     </properties>
  </configuration>
</plugin> 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
执行命令生成单元测试代码
注意:Evosuite 是基于编译后的字节码来生成单测代码的,因此源代码必须先进行编译,然后才能使用evosuite.

示例1:
mvn evosuite:help -Ddetail=true -Dgoal=generate
示例2:
mvn evosuite:generate -Dcuts=com.isoftstone.pcis.service.impl.ApiServiceImpl 
#注意:当工程代码量大时,生成花费的时间可能很长。
示例3:
mvn compile -DmemoryInMB=2000 -Dcores=2 -DtargetFolder=src/test/java/evosuite evosuite:generate evosuite:export test
注意:当工程代码量大时,生成花费的时间可能很长。
1
2
3
4
5
6
7
8
可用参数和常用命令说明:
a. -DmemoryInMB=2000 表示使用 2000MB 的内存
b. -Dcores=2 表示用2个 cpu 来并行加快生成速度
c. -Dcuts=packageName.className 表示只针对指定类生成用例。多个用例可以用英文逗号分隔
d. -DtargetFolder=src/test/java/evosuite 表示生成的用例放到 src/test/java/evosuite 。
e. 常用命令
e1. prepare:需要同时运行EvoSuite测试和现有测试mvn evosuite:prepare test
e2. evosuite:generate 表示执行生成用例
e3. evosuite:export 表示导出用例到 targetFolder 的值所在的目录中(默认值为“ src / test / java”)
e4. evosuite:clean:删除“ .evosuite”文件夹中的所有数据,该文件夹用于存储到目前为止生成的所有最佳测试
具体更多详情可evosuite:help进行查看

执行完毕后,在 .evosuite 的目录下会生成测试文件
针对一个类会生成两份测试文件: _ESTest.java包含单测用例,_scaffolding.java 用例基类,用来在测试前初始化Evosuite的沙盒环境。
效果评估
a. 可以看到 Evosuite 会自动对 servce 依赖的其他对象进行 mock。
b. 针对被测方法的参数,根据参数类型会使用各种边界值进行测试。使用 evoSuite 生成的单测代码覆盖率可以达到:方法覆盖100%,行覆盖51%。
c. 工具存在的不足之处: Mock 对象的方法调用只能返回空值 null 等, 没有对方法进行特定的 stub ,因此正常的逻辑无法走到。EvoSuite 生成的单测用例更适用于测试边界情况和异常情况。正常场景还是得靠人

2.3 eclipse 插件
EvoSuite 插件需要 Java 8 的运行环境,并且只支持 Eclipse 的 Lunar 和 Mars 版本,若系统中有多种 Java 开发环境,需将 Eclipse 的默认 jre 设置成 Java 8 版本。参考

插件安装,并重启Eclipse
插件参数设置
选中需要测试的类,右击鼠标,选择Generate tests with EvoSuite
Junit测试报告的生成

2.4 idea 插件
插件安装,并重启IDEA
选中需要测试的类,右击鼠标,选择Run EvoSuite
在参数输入对话框中,设置参数并执行参考

三、其他类似扩展
商业工具:AgitarOne,Jtest对应zhihu
免费工具:CodePlex AnalytiX,EvoSuite,Randoop, JUnitGenerator V2.0(IDEA plugins)
————————————————
https://www.likecs.com/show-40143.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章