EvoSuite是由Sheffield等大學聯合開發的一種開源工具,用於自動生成測試用例集,生成的測試用例均符合Junit的標準,可直接在Junit中運行。獲得了Google和Yourkit的支持。java
本次全部內容的例子能夠經過測者公衆號後臺,回覆evo_mvn關鍵字獲取下載地址apache
解壓到本身的工程目錄,在 Tutorial_Maven
的示例目錄運行以下命令:bash
mvn compile複製代碼
完成compile後代碼中的classes的編譯字節碼放到target/classes目錄下。在示例代碼中 src/test/java目錄下是有一些測試cases,能夠經過以下命令運行一下:併發
mvn test複製代碼
若是運行ok,那麼說明配置一切正常,就能夠開始集成EvoSuite了。maven
------------------------------------------------------- T E S T S ------------------------------------------------------- Running tutorial.StackTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.094 sec複製代碼
要使用EvoSuite,就要在Maven工程的pom.xml文件中引入EvoSuite的插件以下(在 <project>
的子節點加入以下內容):工具
<build> <plugins> <plugin> <groupId>org.evosuite.plugins</groupId> <artifactId>evosuite-maven-plugin</artifactId> <version>1.0.6</version> </plugin> </plugins> </build>複製代碼
Maven自動的就會下載EvoSuite的相關依賴,若是加入後,出現錯誤,那麼須要加入EvoSuite的Maven Respository。性能
<pluginRepositories> <pluginRepository> <id>EvoSuite</id> <name>EvoSuite Repository</name> <url>http://www.evosuite.org/m2</url> </pluginRepository></pluginRepositories>複製代碼
完成後,經過以下命令進行一下evosuite的測試。測試
mvn evosuite:help複製代碼
第一次使用EvoSuite插件,Maven會下來和EvoSuite相關的全部依賴,下載完成後,出現以下類似信息表示配置成功:ui
[INFO] --- evosuite-maven-plugin:1.0.6:help (default-cli) @ Tutorial_Maven ---[INFO] Maven Plugin for EvoSuite 1.0.6 Plugin used to run EvoSuite to automatically generate high coverage JUnit tests This plugin has 7 goals: evosuite:clean Remove all local files created by EvoSuite so far evosuite:coverage Execute the manually written test suites (usually located under src/test/java) and return the coverage of each class. evosuite:export When run, EvoSuite generate tests in a specific folder. New runs of EvoSuite can exploit the tests in such folder, and/or modify them. So, with 'export' we can copy all generated tests to a specific folder, which by default points to where Maven searches for tests. If another folder is rather used (or if we want to run with Maven the tests in the default EvoSuite folder), then Maven plugins like build-helper-maven-plugin are neededevosuite:generate Generate JUnit testsevosuite:help Display help information on evosuite-maven-plugin. Call mvn evosuite:help -Ddetail=true -Dgoal=<goal-name> to display parameter details.evosuite:info Obtain info of generated tests so farevosuite:prepare Workaround mojo to overcome bug in Maven. Needed when EvoSuite tests are run together with manual ones[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 1.489 s[INFO] Finished at: 2016-04-04T10:55:45+01:00[INFO] Final Memory: 9M/109M[INFO] ------------------------------------------------------------------------複製代碼
配置完成後,運行以下命令生成測試代碼和mock數據:this
mvn evosuite:generate複製代碼
這有可能須要一段時間,若是電腦性能比較好,能夠經過參數設置多併發的generate:
mvn -Dcores=4 evosuite:generate複製代碼
完成後,能夠看到生成了一個.evosuite的目錄,裏面的best-test就是你須要的代碼了。能夠經過以下的命令,查看一下generate腳本的信息怎麼樣:
mvn evosuite:info複製代碼
返回信息以下:
As we have just invoked EvoSuite on 4 classes, you should get an output like this:[INFO] --- evosuite-maven-plugin:1.0.6:info (default-cli) @ Tutorial_Maven ---[INFO] Going to query EvoSuite info on current project[INFO] * EvoSuite 1.0.6[INFO] Total number of classes in the project: 4[INFO] Number of classes in the project that are testable: 4[INFO] Number of generated test suites: 4[INFO] Overall coverage: 0.99[INFO] ------------------------------------------------------------------------複製代碼
Maven項目的JUnit的測試cases通常都是放在src/test/java下,可是EvoSuite生成實在.evosuite下,那麼經過以下命令就能夠完成對應腳本的而移動了。
mvn evosuite:export複製代碼
要想經過 mvn test
命令執行測試,還須要在pom的 <dependencies></dependencies>
內加入以下內容:
<dependency> <groupId>org.evosuite</groupId> <artifactId>evosuite-standalone-runtime</artifactId> <version>1.0.6</version> <scope>test</scope></dependency>複製代碼
經過以下命令能夠制定腳本的移動位置
mvn evosuite:export -DtargetFolder=src/test/evosuite複製代碼
或者,經過添加properites制定移動位置
src/test/evosuite
若果在項目中配置了腳本的移動目錄,要再次使用mvn test就會報錯,所以須要顯示指出測試腳本的位置:
<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>${targetFolder}</source> </sources> </configuration> </execution> </executions></plugin>複製代碼
有時候,咱們會同時執行兩類腳本,一類是RD手寫的代碼,一類是EvoSuite自動生成的,進入同時測試並不會出現什麼大問題,可是也會對測試結果有片面的影響,所以須要只能EvoSuite僅對其生成的腳本起做用,須要在pom中加入以下插件。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.17</version> <configuration> <properties> <property> <name>listener</name> <value>org.evosuite.runtime.InitializingListener</value> </property> </properties> </configuration></plugin>複製代碼