测者陈磊

测者的测试技术手册:自动的自动化EvoSuite 自动生成JUn

2018-12-14  本文已影响3人  Criss陈磊

EvoSuite简介

EvoSuite是由Sheffield等大学联合开发的一种开源工具,用于自动生成测试用例集,生成的测试用例均符合Junit的标准,可直接在Junit中运行。得到了Google和Yourkit的支持。

intelliJ IDEA插件

打开IDE,进入setting(mac版本是Preferences),选择plugins,点击Browse repositories,搜索EvoSuite Plugin,然后选择Install plugin。

Maven 插件引入

当前支持Maven3.1以上版本。

Maven工程可以通过引入EvoSuite的Maven插件来生成新的测试用例。使用Maven插件有如下好处:

1、可以和Jenkins结合,方便快速的运行EvoSuite

2、测试用例生成在pom.xml文件约定好的工程目录下

3、通过Maven的依赖引入EvoSuite,无需单独下载独立的jar文件。

插件的引入如下:

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

</plugins>

</pluginManagement>

引入依赖:

<dependency>

 <groupId>org.evosuite</groupId>

 <artifactId>evosuite-standalone-runtime</artifactId>

 <version>${evosuiteVersion}</version>

 <scope>test</scope>

</dependency>

设定版本的变量(最新版可以在http://www.evosuite.org/downloads/查询):

<properties>

  <evosuiteVersion>1.0.6</evosuiteVersion>

</properties>

由于EvoSuite是生成的JUnit的文件,因此需要引入Junit的依赖。

<dependency>

 <groupId>junit</groupId>

 <artifactId>junit</artifactId>

 <version>4.12</version>

 <scope>test</scope>

</dependency>

EvoSuite的使用

EvoSuite的插件将会对对应的子模块的所有的类进行测试用例生成分析,再分析前需要保证对应代码是build过的。通过插件选取或者mvn compile evosuite:generate 开始分析。

默认情况下会在模块目录下生成.evosuite目录,目录里面是测试用例,如果想要修改那么可以通过如下插件进行配置

<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

详情参见官方文档:http://www.evosuite.org/documentation/

关注测者,关注测试

上一篇 下一篇

猜你喜欢

热点阅读