单元测试自动生成工具evosuite

github地址:https://github.com/EvoSuite/evosuite
官网地址:http://www.evosuite.org
 
快速开始:
1.  junit 
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
  </dependency>
</dependencies>
 
2.添加 evosuite 插件
<plugin>
    <groupId>org.evosuite.plugins</groupId>
    <artifactId>evosuite-maven-plugin</artifactId>
    <version>1.0.6</version>
  </plugin>
evosuite 的仓库
<pluginRepositories>
  <pluginRepository>
    <id>EvoSuite</id>
    <name>EvoSuite Repository</name>
    <url>http://www.evosuite.org/m2</url>
  </pluginRepository>
</pluginRepositories> 
3.evosuite 的运行依赖
<dependency>
    <groupId>org.evosuite</groupId>
    <artifactId>evosuite-standalone-runtime</artifactId>
    <version>1.0.6</version>
    <scope>test</scope>
</dependency>
4.开始生成

mvn evosuite:generate
mvn compile -DmemoryInMB=2000 -Dcores=2 -Dcuts=com.xxx.xxx.api.service.impl.CurriculumServiceImpl-DtargetFolder=src/test/java/evosuite evosuite:generate evosuite:export
compile 表示编译。evosuite 是基于编译后的 .class 文件生成用例的,所以需要先编译。
-DmemoryInMB=2000 表示使用 2000MB 的内存
-Dcores=2 表示用2个 cpu 来并行加快生成速度
-Dcuts=com.xxx.xxx.api.service.impl.CurriculumServiceImpl 表示只针对 com.xxx.xxx.api.service.impl.CurriculumServiceImpl 这个类生成用例。多个用例可以用英文逗号分隔
-DtargetFolder=src/test/java/evosuite 表示生成的用例放到 src/test/java/evosuite
evosuite:generate 表示执行生成用例
evosuite:export 表示导出用例到 targetFolder 的值所在的目录中
 
Idea插件使用
http://www.evosuite.org/documentation/intellij-idea-plugin/
http://www.evosuite.org/documentation/maven-plugin/
 
1.下载 evosuite-1.0.6 jar
2.idea安装evosuite插件
3.配置


2.evosuite -jacoco
http://www.evosuite.org/documentation/measuring-code-coverage/
https://segmentfault.com/a/1190000017284529?utm_medium=referral&utm_source=tuicool
注意:EvoSuite的bytecode instrumentation和类似Jacoco这类工具在bytecode instrumentation上有可能有冲突,导致最后的覆盖率统计都是0(修改separateClassLoader = true为false)

<plugins>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-report-plugin</artifactId>
          <version>3.0.0-M3</version>
      </plugin>
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.0</version>
        <configuration>
          <excludes>//排出不需要收集覆盖率的
             <exclude>**/*ClazzSearchController.class</exclude>
             <exclude>**/*Application.class</exclude>
             <exclude>com/xxx/xxxx/api/config/**/*</exclude>
          </excludes>
        </configuration>
        <executions>
        <!--                  <execution>-->
<!--                      <id>prepare-agent</id>-->
<!--                      <goals>-->
<!--                          <goal>prepare-agent</goal>-->
<!--                      </goals>-->
<!--                  </execution>-->
<!--                  <execution>-->
<!--                      <id>report</id>-->
<!--                      <phase>prepare-package</phase>-->
<!--                      <goals>-->
<!--                          <goal>report</goal>-->
<!--                      </goals>-->
<!--                  </execution>-->
<!--                  <execution>-->
<!--                      <id>post-unit-test</id>-->
<!--                      <phase>test</phase>-->
<!--                      <goals>-->
<!--                          <goal>report</goal>-->
<!--                      </goals>-->
<!--                      <configuration>-->
<!--                          <dataFile>target/jacoco.exec</dataFile>-->
<!--                          <outputDirectory>target/jacoco-ut</outputDirectory>-->
<!--                      </configuration>-->
<!--                  </execution>-->
                  <execution>
                      <id>prepare-agent</id>
                      <goals>
                          <goal>prepare-agent</goal>
                      </goals>
                  </execution>
                  <execution>
                      <id>generate-code-coverage-report</id>
                      <phase>test</phase>
                      <goals>
                          <goal>report</goal>
                      </goals>
                  </execution>
        </executions>
    </plugin>
</plugins>
 
2.执行命令
mvn jacoco:prepare-agent test jacoco:report
 
单元测试自动生成工具evosuite

 3.查看测试报告

单元测试自动生成工具evosuite
单元测试自动生成工具evosuite

相关推荐