EvoSuite是由Sheffield等大学联合开发的一种开源工具,用于自动生成测试用例集,生成的测试用例均符合Junit的标准,可直接在Junit中运行。得到了Google和Yourkit的支持。
前期准备
本次所有内容的例子可以通过测者公众号后台,回复evo_mvn关键字获取下载地址
解压到自己的工程目录,在 Tutorial_Maven
的示例目录运行如下命令:
-
mvn compile
完成compile后代码中的classes的编译字节码放到target/classes目录下。在示例代码中 src/test/java目录下是有一些测试cases,可以通过如下命令运行一下:
-
mvn test
如果运行ok,那么说明配置一切正常,就可以开始集成EvoSuite了。
-
-------------------------------------------------------
-
T E S T S
-
-------------------------------------------------------
-
Running tutorial.StackTest
-
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.094 sec
加入EvoSuite生成Junit的Cases
要使用EvoSuite,就要在Maven工程的pom.xml文件中引入EvoSuite的插件如下(在 <project>
的子节点加入如下内容):
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.evosuite.plugins</groupId>
-
<artifactId>evosuite-maven-plugin</artifactId>
-
<version>1.0.6</version>
-
</plugin>
-
</plugins>
-
</build>
Maven自动的就会下载EvoSuite的相关依赖,如果加入后,出现错误,那么需要加入EvoSuite的Maven Respository。
-
<pluginRepositories>
-
<pluginRepository>
-
<id>EvoSuite</id>
-
<name>EvoSuite Repository</name>
-
<url>http://www.evosuite.org/m2</url>
-
</pluginRepository>
-
</pluginRepositories>
完成后,通过如下命令进行一下evosuite的测试。
-
mvn evosuite:help
第一次使用EvoSuite插件,Maven会下来和EvoSuite相关的所有依赖,下载完成后,出现如下相似信息表示配置成功:
-
[INFO] --- evosuite-maven-plugin:1.0.6:help (default-cli) @ Tutorial_Maven ---
-
[INFO] Maven Plugin for EvoSuite 1.0.6
-
Plugin used to run EvoSuite to automatically generate high coverage JUnit
-
tests
-
This plugin has 7 goals:
-
evosuite:clean
-
Remove all local files created by EvoSuite so far
-
evosuite:coverage
-
Execute the manually written test suites (usually located under src/test/java)
-
and return the coverage of each class.
-
evosuite:export
-
When run, EvoSuite generate tests in a specific folder. New runs of EvoSuite
-
can exploit the tests in such folder, and/or modify them.
-
So, with 'export' we can copy all generated tests to a specific folder, which
-
by default points to where Maven searches for tests. If another folder is
-
rather used (or if we want to run with Maven the tests in the default EvoSuite
-
folder), then Maven plugins like build-helper-maven-plugin are needed
-
evosuite:generate
-
Generate JUnit tests
-
evosuite:help
-
Display help information on evosuite-maven-plugin.
-
Call mvn evosuite:help -Ddetail=true -Dgoal=<goal-name> to display parameter
-
details.
-
evosuite:info
-
Obtain info of generated tests so far
-
evosuite:prepare
-
Workaround mojo to overcome bug in Maven. Needed when EvoSuite tests are run
-
together with manual ones
-
[INFO] ------------------------------------------------------------------------
-
[INFO] BUILD SUCCESS
-
[INFO] ------------------------------------------------------------------------
-
[INFO] Total time: 1.489 s
-
[INFO] Finished at: 2016-04-04T10:55:45+01:00
-
[INFO] Final Memory: 9M/109M
-
[INFO] ------------------------------------------------------------------------
配置完成后,运行如下命令生成测试代码和mock数据:
-
mvn evosuite:generate
这有可能需要一段时间,如果电脑性能比较好,可以通过参数设置多并发的generate:
-
mvn -Dcores=4 evosuite:generate
完成后,可以看到生成了一个.evosuite的目录,里面的best-test就是你需要的代码了。可以通过如下的命令,查看一下generate脚本的信息怎么样:
-
mvn evosuite:info
返回信息如下:
-
As we have just invoked EvoSuite on 4 classes, you should get an output like this:
-
[INFO] --- evosuite-maven-plugin:1.0.6:info (default-cli) @ Tutorial_Maven ---
-
[INFO] Going to query EvoSuite info on current project
-
[INFO] * EvoSuite 1.0.6
-
[INFO] Total number of classes in the project: 4
-
[INFO] Number of classes in the project that are testable: 4
-
[INFO] Number of generated test suites: 4
-
[INFO] Overall coverage: 0.99
-
[INFO] ------------------------------------------------------------------------
Maven项目的JUnit的测试cases一般都是放在src/test/java下,但是EvoSuite生成实在.evosuite下,那么通过如下命令就可以完成对应脚本的而移动了。
-
mvn evosuite:export
mvn test执行测试
要想通过 mvn test
命令执行测试,还需要在pom的 <dependencies></dependencies>
内加入如下内容:
-
<dependency>
-
<groupId>org.evosuite</groupId>
-
<artifactId>evosuite-standalone-runtime</artifactId>
-
<version>1.0.6</version>
-
<scope>test</scope>
-
</dependency>
EvoSuite的脚本和RD的脚本分开存放
通过如下命令可以制定脚本的移动位置
-
mvn evosuite:export -DtargetFolder=src/test/evosuite
或者,通过添加properites制定移动位置
src/test/evosuite
若果在项目中配置了脚本的移动目录,要再次使用mvn test就会报错,因此需要显示指出测试脚本的位置:
-
<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>${targetFolder}</source>
-
</sources>
-
</configuration>
-
</execution>
-
</executions>
-
</plugin>
RD的Junit脚本和EvoSuite的脚本同时执
有时候,我们会同时执行两类脚本,一类是RD手写的代码,一类是EvoSuite自动生成的,进入同时测试并不会出现什么大问题,但是也会对测试结果有片面的影响,因此需要只能EvoSuite仅对其生成的脚本起作用,需要在pom中加入如下插件。
-
<plugin>
-
<groupId>org.apache.maven.plugins</groupId>
-
<artifactId>maven-surefire-plugin</artifactId>
-
<version>2.17</version>
-
<configuration>
-
<properties>
-
<property>
-
<name>listener</name>
-
<value>org.evosuite.runtime.InitializingListener</value>
-
</property>
-
</properties>
-
</configuration>
-
</plugin>