Evosuite用maven构建
【参考来源】http://www.evosuite.org/documentation/tutorial-part-2/
!如果在照着做出现找不到文件的情况,请翻到本文最后看最终的pom.xml配置以及文件结构
pom.xml
<?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>groupId</groupId>
<artifactId>JdbcDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>org.evosuite</groupId>
<artifactId>evosuite-standalone-runtime</artifactId>
<version>${
evosuiteVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.evosuite.plugins</groupId>
<artifactId>evosuite-maven-plugin</artifactId>
<version>${
evosuiteVersion}</version>
<executions><execution>
<goals> <goal> prepare </goal> </goals>
<phase> process-test-classes </phase>
</execution></executions>
</plugin>
<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>
<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>
</plugins>
</pluginManagement>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<evosuiteVersion>1.0.6</evosuiteVersion>
<customFolder>src/test/java</customFolder>
</properties>
</project>
The EvoSuite Maven plugin命令简介
1)generate
这用于通过EvoSuite生成测试用例。 将为所有子模块中的所有类生成测试。 您需要确保代码已编译。
mvn compile evosuite:generate
可以跟的参数
memoryInMB:EvoSuite允许分配的总兆字节数(默认为800)
cores:EvoSuite可以使用的CPU内核总数(默认为1)
timeInMinutesPerClass:EvoSuite可以花多少分钟为每个类生成测试(默认2)
2)info:提供到目前为止所有已生成测试的信息
3)export: 默认情况下,EvoSuite在“ .evosuite”文件夹中创建测试。 通过使用“导出”,将生成的测试复制到另一个文件夹,该文件夹可以使用“ targetFolder”选项设置(默认值为“ src / test / java”)。
如果不使用“ mvn evosuite:export”将测试导出到“ src / test / java”,则“ mvn test”之类的命令将不会执行这些测试,因为它们的源代码不在构建路径中。 您可以使用“ build-helper-maven-plugin”插件添加自定义源文件夹.
4)clean:删除“ .evosuite”文件夹中的所有数据,该文件夹用于存储到目前为止生成的所有最佳测试.
5)prepare:需要同时运行EvoSuite测试和现有测试.
mvn evosuite:prepare test
最好仅将evosuite插件配置为始终运行,如前所述。
eg.
mvn -DmemoryInMB=2000 -Dcores=2 evosuite:generate evosuite:export test
这将使用2个内核和2GB内存为所有类生成测试,将生成的测试复制到“ src / test / java”,然后执行它们。 注意:如果项目已经进行了一些测试,那么这些测试将作为常规“测试”阶段的一部分执行。
maven集成
准备被测项目
下载被测项目
wget http://evosuite.org/files/tutorial/Tutorial_Maven.zip
unzip Tutorial_Maven.zip
cd Tutorial_Maven
和以前一样,我们可以使用以下命令来编译项目中的类
mvn compile
目录如下
然后这个项目本身有测试,位于JdbcDemo/src/test/java/StackTest.java
这个属于已有测试,可以使用命令
mvn test
更近一步看pom.xml
如果您已经非常熟悉Maven,那么本节可能不会告诉您任何新内容。 但是,了解如何配置Maven项目以配置项目以使用EvoSuite至关重要。 因此,我们现在将仔细研究示例项目的主要Maven配置文件,即文件pom.xml。
我们的示例项目中的pom.xml基于使用mvn archetype:generate生成的基本版本。 它从有关该项目的一些基本元信息开始
查看help命令
mvn evosuite:help
[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
Mojo needed to prepare the EvoSuite tests for execution. This is needed to
make sure that bytecode is properly instrumented.
我们可以使用-Dproperty = value语法为插件目标设置属性,就像为任何Java进程设置属性一样。 例如,要获取有关在执行帮助插件目标时生成插件目标的更多详细信息,我们可以运行以下命令
mvn evosuite:help -Ddetail=true -Dgoal=generate
如果运行此命令,应该会看到可以为生成插件目标设置的所有属性的列表。这个是显示generate参数
evosuite:generate
Generate JUnit tests
Available parameters:
criterion (Default:
LINE:BRANCH:EXCEPTION:WEAKMUTATION:OUTPUT:METHOD:METHODNOEXCEPTION:CBRANCH)
//行:语句:异常:弱变异:输出:方法:方法异常:分支结构
Coverage criterion. Can define more than one criterion by using a ':'
separated list
User property: criterion
// 覆盖准则,可以定义不止一个覆盖准则,使用:隔开
cuts
Comma ',' separated list of CUTs to use in CTG. If none specified, then
test all classes
User property: cuts
// 用逗号“,”分隔CTG中要使用的CUT(被测类)列表。 如果未指定,则测试所有类
cutsFile
Absolute path to a file having the list of CUTs specified. This is needed
for operating systems like Windows that have constraints on the size of
input parameters and so could not use 'cuts' parameter instead if too many
CUTs are specified
User property: cutsFile
// 指定了CUT列表的文件的绝对路径。 对于Windows等大小受限制的操作系统,这是必需的
输入参数,因此如果指定了太多的CUT,则不能使用“ cuts”参数
extraArgs (Default: )
User property: extraArgs
memoryInMB (Default: 800)
Total Memory (in MB) that CTG will use
User property: memoryInMB
numberOfCores (Default: 1)
Number of cores CTG will use
User property: cores
schedule (Default: BUDGET)
Schedule used to run CTG (SIMPLE, BUDGET, SEEDING, BUDGET_AND_SEEDING,
HISTORY)
User property: schedule
spawnManagerPort (Default: )
User property: spawnManagerPort
timeInMinutesPerClass (Default: 2)
How many minutes to allocate for each class
User property: timeInMinutesPerClass
//每个类分配多少分钟
timeInMinutesPerProject (Default: 0)
How many minutes to allocate for each project/module. If this parameter is
not set, then the total time will be timeInMinutesPerClass x
number_of_classes
User property: timeInMinutesPerProject
//为每个项目/模块分配多少分钟。 如果未设置此参数,则总时间为timeInMinutesPerClass x
现在让我们使用evosuite生成些测试
mvn evosuite:generate
[INFO] * EvoSuite 1.0.6
[INFO] Registered remote process from /127.0.0.1:59667
[INFO] Going to execute 10 jobs
[INFO] Estimated completion time: 20 minutes, by 2019-10-10T09:46:48.318
[INFO] Going to start job for: jdbc.Demo04. Expected to end in 179 seconds, by 2019-10-10T09:29:47.340
[INFO] Registered remote process from /127.0.0.1:59671
[INFO] Registered remote process from /127.0.0.1:59677
[INFO] Completed job. Left: 9
[INFO] Going to start job for: jdbc.Demo06. Expected to end in 170 seconds, by 2019-10-10T09:31:21.353
[INFO] Registered remote process from /127.0.0.1:59730
[INFO] Registered remote process from /127.0.0.1:59737
[INFO] Completed job. Left: 8
[INFO] Going to start job for: jdbc.Demo05. Expected to end in 143 seconds, by 2019-10-10T09:32:28.330
[INFO] Registered remote process from /127.0.0.1:59780
[INFO] Registered remote process from /127.0.0.1:59786
[INFO] Completed job. Left: 7
[INFO] Going to start job for: jdbc.Demo03. Expected to end in 133 seconds, by 2019-10-10T09:33:43.435
[INFO] Registered remote process from /127.0.0.1:59819
[INFO] Registered remote process from /127.0.0.1:59825
[INFO] Completed job. Left: 6
[INFO] Going to start job for: jdbc.Demo02. Expected to end in 133 seconds, by 2019-10-10T09:34:58.170
[INFO] Registered remote process from /127.0.0.1:59863
[INFO] Registered remote process from /127.0.0.1:59869
[INFO] Completed job. Left: 5
[INFO] Going to start job for: Tutorial_Maven.LinkedList. Expected to end in 115 seconds, by 2019-10-10T09:35:55.028
[INFO] Registered remote process from /127.0.0.1:59906
[INFO] Registered remote process from /127.0.0.1:59912
[INFO] Completed job. Left: 4
[INFO] Going to start job for: jdbc.Demo01. Expected to end in 96 seconds, by 2019-10-10T09:36:43.878
[INFO] Registered remote process from /127.0.0.1:59946
[INFO] Registered remote process from /127.0.0.1:59953
[INFO] Completed job. Left: 3
[INFO] Going to start job for: Tutorial_Maven.Stack. Expected to end in 87 seconds, by 2019-10-10T09:37:30.592
[INFO] Registered remote process from /127.0.0.1:59981
[INFO] Registered remote process from /127.0.0.1:59987
[INFO] Completed job. Left: 2
[INFO] Going to start job for: Tutorial_Maven.LinkedListIterator. Expected to end in 78 seconds, by 2019-10-10T09:38:13.216
[INFO] Registered remote process from /127.0.0.1:60005
[INFO] Registered remote process from /127.0.0.1:60011
[INFO] Completed job. Left: 1
[INFO] Going to start job for: Tutorial_Maven.Node. Expected to end in 60 seconds, by 2019-10-10T09:38:43.341
[INFO] Registered remote process from /127.0.0.1:60043
[INFO] Registered remote process from /127.0.0.1:60049
[INFO] Completed job. Left: 0
[INFO] * Updating database to Tutorial_Maven.Node
[INFO] * Updating database to Tutorial_Maven.LinkedListIterator
[INFO] * Updating database to jdbc.Demo01
[INFO] * Updating database to Tutorial_Maven.LinkedList
[INFO] * Updating database to jdbc.Demo05
[INFO] * Updating database to jdbc.Demo04
[INFO] * Updating database to Tutorial_Maven.Stack
[INFO] * Updating database to jdbc.Demo03
[INFO] * Updating database to jdbc.Demo02
[INFO] * Updating database to jdbc.Demo06
[INFO] === CTG run results ===
[INFO] Removed test suites: 0
[INFO] New test suites: 10
[INFO] Stopping spawn process manager
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11:10 min
[INFO] Finished at: 2019-10-10T09:37:49+08:00
[INFO] ------------------------------------------------------------------------
我的电脑烫的。。。,让我们来看看这个优秀的工具生成的测试用例,生成的没有成功导出到src/main/test/java,暂时先在目录~/IdeaProjects/JdbcDemo/.evosuite/best-tests下看
为了对比人工和自动生成的,我们就看一下Stack的测试类ba
/*
* This file was automatically generated by EvoSuite
* Thu Oct 10 01:36:54 GMT 2019
*/
package Tutorial_Maven;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.evosuite.runtime.EvoAssertions.*;
import Tutorial_Maven.Stack;
import org.evosuite.runtime.EvoRunner;
import org.evosuite.runtime.EvoRunnerParameters;
import org.junit.runner.RunWith;
@RunWith(EvoRunner.class) @EvoRunnerParameters(mockJVMNonDeterminism = true, useVFS = true, useVNET = true, resetStaticState = true, separateClassLoader = true, useJEE = true)
public class Stack_ESTest extends Stack_ESTest_scaffolding {
/**判断栈空测试-1*/
@Test(timeout = 4000)
public void test0() throws Throwable {
Stack<Object> stack0 = new Stack<Object>();
assertTrue(stack0.isEmpty());
stack0.push(stack0);
stack0.push(stack0);
stack0.pop()