測者的測試技術手冊:自動的自動化--voSuite 自動生成JUnit的測試用例
學習了雲測峰會邱化峰的Session,提到的很多技術都超級感興趣,測者嘗試試用了邱哥推薦的EvoSuite,下面介紹一個入門過程。
##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/
轉載於:https://my.oschina.net/leichen/blog/2987394