Maven 单元测试
http://drizzlewalk.blog.51cto.com/2203401/1118536
一、maven-surefire-plugin簡介
????? maven-surefire-plugin支持JUnit和TestNG。默認情況下,maven-surefire-plugin的test目標會自動執行測試源碼路徑下所有以Test開頭、Test或TestCase結尾的的Java類。
二、跳過測試
????如果想跳過測試階段,可用:
1.??mvn?package?-DskipTests
????想臨時性跳過測試代碼的編譯,可用:
1.??mvn?package?-Dmaven.test.skip=true
??? ?maven.test.skip同時控制maven-compiler-plugin和maven-surefire-plugin兩個插件的行為,即跳過編譯,又跳過測試。
三、手動指定測試用例
??? maven-surefire-plugin的test參數用來指定要運行的測試用例:
1.??//指定測試類??
2.??mvn?test?-Dtest=RandomGeneratorTest??
3.??//以Random開頭,Test結尾的測試類??
4.??mvn?test?-Dtest=Random*Test??
5.??//用逗號分隔指定多個測試用例??
6.??mvn?test?-Dtest=ATest,BTest
???? test參數必須匹配至少一個測試類,否則會報錯并導致構建失敗。此時可使用:
1.??mvn?test?-Dtest?-DfailIfNoTests?=?false??
來指定即使沒有任何測試用例也不要報錯。
四、包含與排除測試用例
1.??<plugin>??
2.??????<groupId>org.apahce.maven.plugins<groupId>??
3.??????<artifactId>maven-surefire-plugin</artifactId>??
4.??????<version>2.5</version>??
5.??????<configuration>??
6.??????<includes>??
7.??????????<include>**?/?*Tests.java</include>??
8.??????</includes>??
9.??????</configuration>????
10.?</plugin>
?????使用** / * Test.java?來匹配所有以Tests結尾的Java類。兩個星號**用來匹配任意路徑,一個星號*用來獲取除路徑風格符外的0個或多個字符。還可使用excludes來排除一些測試類。
五、測試報告
1.基本的測試報告
?????默認情況下,maven-surefire-plugin會在項目的target/surefire-reports下生成兩種格式的錯誤報告:
·??????????簡單文本格式?
·?????????與JUnit兼容的XML格式
2.測試覆蓋率報告
??? Cobertura是一個用來測試覆蓋率統計工具。Maven通過cobertura-maven-plugin插件與之集成,命令
1.??mvn?cobertura:cobertura
六、運行TestNG測試
??? TestNG支持使用xml來配置要運行的測試用例。可用:
1.??<configuration>??
2.??????<suiteXmlFiles>??
3.??????<suiteXmlFile>testing.xml</suiteXmlFile>??
4.??????</suiteXmlFiles>??
5.??</configuration>
?? TestNG還支持測試組?
1.??<configuration>??
2.??????<groups>util,medium</groups>??
3.??</configuration>
七、重用測試代碼
????通過配置maven-jar-plugin將測試類打包
1.??<plugin>??
2.??????<groupId>org.apahce.maven.plugins<groupId>??
3.??????<artifactId>maven-jar-plugin</artifactId>??
4.??????<version>2.2</version>??
5.??????<executions>??
6.??????????<execution>??
7.??????????<goals>??
8.??????????<goal>test-jar</goal>??
9.??????????</goals>??
10.?????</execution>??
11.?????</executions>??
12.?</plugin>
????打包后可以聲明依賴:
1.??<groupId></groupId>??
2.??<artifactId></artifactId>??
3.??<version></version>??
4.??<type>test-jar</type>??
5.??<scope>test</scope>?
總結
以上是生活随笔為你收集整理的Maven 单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql中case when then
- 下一篇: iBatis resultMap出错 T