Maven Evosuite

Idea+maven+evosuite

首先是maven中pom.xml的配置:

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
35
36
37
38
39
40
41
42
43
44
45
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.zjn</groupId>
<artifactId>EvotestDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

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

<build>
<finalName>EvotestDemo</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.evosuite.plugins</groupId>
<artifactId>evosuite-maven-plugin</artifactId>
<version>1.0.6</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

“generate”:使用Evosuite产生测试用例,将会生成在所有子模块中对于所有类产生的测试。

1
mvn compile evosuite:generate

后面可以加的参数:

“memoryInMB”:EvoSuite允许分配的总兆字节数(默认为800)。

“cores”:EvoSuite可以使用的CPU核心总数(默认值为1)

“timeInMinutesPerClass”:EvoSuite可以花多少分钟为每个类生成测试(默认为2)

“info” :提供迄今为止所有生成的测试的信息

“export”:默认情况下,Evosuite创建的测试位于“.evosuite”文件夹下,通过使用”export”,产生的测试会复制到另一个文件夹,即可以设置”targetFolder”选项,默认值为”src/test/java”

注意:如果你不进行上述export的操作,那么”mvn test”将不会执行这些测试,他们的源代码就不会创建路径,你可以通过“build-helper-maven-plugin”插件增加用户源目录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${customFolder}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

如果${customFolder}等于”.evosuite/evosuite-tests”,这样你就不用使用”evosuite:export”命令了

“clean”删除所有在”.evosuite”文件夹下的数据。

“prepare”需要运行与现有测试混合的EvoSuite测试

运行evosuite:

1
mvn compile -DtargetFolder=src/test/java/evosuite evosuite:generate evosuite:export

运行成功的截图:

1544148554440

运行后在“src/test/java/evosuite”目录下生成了两个文件:

1544148514110

参考:单元测试自动生成工具 evosuite 尝鲜

PostServiceImpl_ESTest.java:测试用例文件
PostServiceImpl_ESTest_scaffolding.java:用例基类,用于在开始测试前初始化 evosuite 的沙盒机制

对于测试的有效性和覆盖率后待验证。