Evosuite, a Java unit test code generation tool

EvoSuite is an open source tool jointly developed by universities such as Sheffield, which is used to automatically generate test case sets. The generated test cases are all in line with Junit standards and can be run directly in Junit.
By using this automatic testing tool, the development efficiency of testers can be greatly improved under the premise of ensuring code coverage. However, it can only assist testing, and cannot completely replace manual work. Whether the test cases are correct or not requires manual judgment.
Official website address: https://www.evosuite.org/contact/
github address: https://github.com/EvoSuite/evosuite

A core function:

For the specified class, generate the single test code of Junit4 type. ------Generation of JUnit 4 tests for the selected classes
Adjust the generated use cases according to different coverage metrics, such as line coverage, branch coverage, output coverage, etc. ------Optimization of different coverage criteria, like lines, branches, outputs and mutation testing
The single test cases are minimized, and only the single test cases that contribute to the coverage will be retained. ------Tests are minimized: only the ones contributing to achieve coverage are retained
The generated unit test cases contain Junit assertions. ------Generation of JUnit asserts to capture the current behavior of the tested classes
Tests are run in a sandbox. ------Tests run in a sandbox to prevent potentially dangerous operations
Virtual file system. ------Virtual file system
virtual network. ------Virtual network

Two, use

The official provides several operating modes including command line tools, eclipse plug-ins, idea plug-ins, and maven plug-ins.
Refer to the official http://www.evosuite.org/documentation/tutorial-part-2/

2.1 Command line tools

The command line enters the project root directory /target/classes/ directory and enters the command:

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

Parameter description:
-class: the object to execute
-projectCP: set the class path generated by the test
-help: to view the available command line options
-criterion: the standard parameters of the test are (line, branch, cbranch, mutation, exception, etc.)
-Dminimize =false //It will delete all statements that do not meet the coverage target
-Dassertion_strategy=all //Use a large number of assertions to generate long tests
for more parameters Please refer to the development documentation: http://www.evosuite.org/documentation/tutorial -part-1/

2.2 maven plugin

Adjust the junit version (above Junit4.12), and increase the evosuite operation dependency

<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> 

Execute the command to generate unit test code
Note: Evosuite generates unit test code based on the compiled bytecode, so the source code must be compiled before evosuite can be used.

示例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
注意:当工程代码量大时,生成花费的时间可能很长。

Description of available parameters and common commands:
a. -DmemoryInMB=2000 means use 2000MB of memory
b. -Dcores=2 means use 2 cpus to parallelize the generation speed
c. -Dcuts=packageName.className means only generate use cases for the specified class. Multiple use cases can be separated by English commas
d. -DtargetFolder=src/test/java/evosuite indicates that the generated use cases are placed in src/test/java/evosuite.
e. Commonly used commands
e1. prepare: need to run EvoSuite test and existing test at the same time mvn evosuite:prepare test
e2. evosuite:generate means to execute the generated use case
e3. evosuite:export means to export the use case to the directory where the value of targetFolder is located (default value "src/test/java")
e4.evosuite:clean: Delete all data in the ".evosuite" folder, which is used to store all the best tests generated so far.
More details can be found in evosuite:help to view

After the execution is completed, a test file will be generated in the .evosuite directory.
Two test files will be generated for a class: _ESTest.java contains single test cases, and _scaffolding.java is the base class of use cases, which is used to initialize the sandbox environment of Evosuite before testing .
Effect evaluation
a. You can see that Evosuite will automatically mock other objects that servce depends on.
b. For the parameters of the method under test, various boundary values ​​are used for testing according to the parameter type. The unit test code coverage generated by evoSuite can reach: method coverage 100%, line coverage 51%.
c. The shortcomings of the tool: the method call of the Mock object can only return the null value, etc., and there is no specific stub for the method, so the normal logic cannot be reached. The unit test cases generated by EvoSuite are more suitable for testing edge cases and abnormal cases. Normal scenes still depend on people

2.3 eclipse plug-ins

The EvoSuite plug-in requires a Java 8 operating environment, and only supports the Lunar and Mars versions of Eclipse. If there are multiple Java development environments in the system, you need to set the default jre of Eclipse to the Java 8 version. refer to

Plug-in installation, and restart the Eclipse
plug-in parameter settings
Select the class to be tested, right-click the mouse, and select Generate tests with EvoSuite
Junit test report generation

2.4 idea plugin

Plug-in installation, and restart IDEA
Select the class to be tested, right-click the mouse, select Run EvoSuite
In the parameter input dialog box, set the parameters and execute the reference

insert image description here

3. Other similar extensions

Commercial tools: AgitarOne, Jtest corresponding to zhihu
Free tools: CodePlex AnalytiX, EvoSuite, Randoop, JUnitGenerator V2.0 (IDEA plugins)

Guess you like

Origin blog.csdn.net/heqiushuang110/article/details/126963984