继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

自动的自动化:EvoSuite 自动生成JUnit的测试用例

慕虎7371278
关注TA
已关注
手记 1125
粉丝 201
获赞 871

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>



作者:TeacherAilie
链接:https://www.jianshu.com/p/642bbf8f5e9d


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP