天天看点

EvoSuite 自动生成代码接入指南

官方网站为:https://www.evosuite.org/

1、官网找到以下链接

EvoSuite 自动生成代码接入指南

2、maven 插件接入

引入org.evosuite.plugins插件

<!--evoSuite 插件-->
<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>
           

并且在属性中加入相应的变量:

<properties>
        <evosuiteVersion>1.0.6</evosuiteVersion>
</properties>
           

加入evosuite运行包

<dependency>
  <groupId>org.evosuite</groupId>
  <artifactId>evosuite-standalone-runtime</artifactId>
  <version>${evosuiteVersion}</version>
  <scope>test</scope>
</dependency>
           

使用EvoSuite的Java代理时,还需要配置surefire插件来运行EvoSuite测试的初始化监听器。当EvoSuite自动测试与手工编写的测试混合使用时。如下所示:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <skipTests>true</skipTests>
        <properties>
            <property>
                <name>listener</name>
                <value>org.evosuite.runtime.InitializingListener</value>
            </property>
        </properties>
    </configuration>
</plugin>
           

如何使用EvoSuite Maven插件提供了以下目标:

1)“generate”->这是用来生成带有EvoSuite的测试用例的。将为所有子模块中的所有类生成测试。您需要确保代码已编译,例如“mvn compile”evosuite:generate”. 此目标为以下参数:“”memoryInMB“:EvoSuite允许分配的兆字节总量(默认值800)”cores“:EvoSuite可以使用的CPU内核总数(默认值1)*”timeInMinutesPerClass“:EvoSuite可以为每个类生成测试花费多少分钟(默认值2)

2)“info”->提供到目前为止生成的所有测试3)“导出”->默认情况下,EvoSuite在“.EvoSuite”文件夹中创建测试。通过使用“export”,生成的测试将被复制到另一个文件夹,该文件夹可以通过“targetFolder”选项设置(默认值为“src/test/java”)。注意:如果不将测试导出到带有“mvn”的“src/test/java”中evosuite:export,则像“mvn test”这样的命令将不会执行这些测试,因为它们的源代码不在构建路径上。您可以使用“build helper maven 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>
           

运行使用:

1、如果等于,则不需要使用如果这样做,那么您将得到编译错误,因为每个测试将在类路径上出现两次)。注意:另一种方法是重写以指向customFolder等于“.evosuite/evosuitetests”,则不需要使用evosuite:export”.(如果这样做,那么您将得到编译错误,因为每个测试将在类路径上出现两次)。注意:另一种方法是重写“”以指向{customFolder}。

2、如果希望只在EvoSuite生成的测试上运行“mvn test”(例如,如果在Jenkins上有两个不同的配置/配置文件,一个只运行现有的手动测试,另一个只运行EvoSuite测试),这将非常有用。

3、“clean”->删除“.evosuite”文件夹中的所有数据,该文件夹用于存储迄今为止生成的所有最佳测试。

4、“prepare”->需要运行与现有测试混合的EvoSuite测试,例如“mvn”evosuite:prepare test”. 如前所述,最好只配置evosuite插件以始终运行它。

运行命令如下:

mvn -DmemoryInMB=2000 -Dcores=2 evosuite:generate evosuite:export  test

           

测试代码生成截图:

EvoSuite 自动生成代码接入指南

继续阅读