EvoSuite簡介
EvoSuite是由Sheffield等大學(xué)聯(lián)合開發(fā)的一種開源工具,用于自動生成測試用例集,生成的測試用例均符合Junit的標(biāo)準(zhǔn),可直接在Junit中運行。得到了Google和Yourkit的支持。
intelliJ IDEA插件
打開IDE,進(jìn)入setting(mac版本是Preferences),選擇plugins,點擊Browse repositories,搜索EvoSuite Plugin,然后選擇Install plugin。
Maven 插件引入
當(dāng)前支持Maven3.1以上版本。
Maven工程可以通過引入EvoSuite的Maven插件來生成新的測試用例。使用Maven插件有如下好處:
1、可以和Jenkins結(jié)合,方便快速的運行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>
設(shè)定版本的變量(最新版可以在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的插件將會對對應(yīng)的子模塊的所有的類進(jìn)行測試用例生成分析,再分析前需要保證對應(yīng)代碼是build過的。通過插件選取或者mvn compile evosuite:generate 開始分析。
默認(rèn)情況下會在模塊目錄下生成.evosuite目錄,目錄里面是測試用例,如果想要修改那么可以通過如下插件進(jìn)行配置
<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,那么不需要再次執(zhí)行evosuite:export
詳情參見官方文檔:http://www.evosuite.org/documentation/
關(guān)注測者,關(guān)注測試