maven install時自動執行單元測試
1.maven-surefire-plugin簡介
Maven本身並不是一個單元測試框架,它只是在構建執行到特定生命周期階段的時候,通過插件來執行JUnit或者TestNG的測試用例。這個插 件就是maven-surefire-plugin,也可以稱為測試運行器(Test Runner),它能兼容JUnit 3、JUnit 4以及TestNG。
在默認情況下,maven-surefire-plugin的test目標會自動執行測試源碼路徑(默認為src/test/java/)下所有符合一組命名模式的測試類。這組模式為:
- **/Test*.java:任何子目錄下所有命名以Test開關的Java類。
- **/*Test.java:任何子目錄下所有命名以Test結尾的Java類。
- **/*TestCase.java:任何子目錄下所有命名以TestCase結尾的Java類。
2.跳過測試
要想跳過
測試,在命令行加入參數skipTests就可以了。如:
- mvn package -DskipTests
也可以在pom配置中提供該屬性。
- < plugin >
- < groupId > org.apache.maven.plugins </ groupId >
- < artifactId > maven-surefire-plugin </ artifactId >
- < version > 2.5 </ version >
- < configuration >
- < skipTests > true </ skipTests >
- </ configuration >
- </ plugin >
- mvn package -Dmaven.test.skip=true
也可以在pom中配置maven.test.skip:
- < plugin >
- < groupId > org.apache.maven.plugin </ groupId >
- < artifactId > maven-compiler-plugin </ artifactId >
- < version > 2.1 </ version >
- < configuration >
- < skip > true </ skip >
- </ configuration >
- </ plugin >
- < plugin >
- < groupId > org.apache.maven.plugins </ groupId >
- < artifactId > maven-surefire-plugin </ artifactId >
- < version > 2.5 </ version >
- < configuration >
- < skip > true </ skip >
- </ configuration >
- </ plugin >
3.動態指定要運行的測試用例
maven-surefire-plugin提供了一個test參數讓Maven用戶能夠在命令行指定要運行的
測試用例。如:
- mvn test -Dtest=RandomGeneratorTest
也可以使用通配符:
- mvn test -Dtest=Random*Test
或者也可以使用“,”號指定多個 測試類:
- mvn test -Dtest=Random*Test,AccountCaptchaServiceTest
如果沒有指定 測試類,那么會報錯並導致構建失敗。
- mvn test -Dtest
這時候可以添加-DfailIfNoTests=false參數告訴maven-surefire-plugin即使沒有任何 測試也不要報錯。
- mvn test -Dtest -DfailIfNoTests=false
由此可見,命令行參數-Dtest -DfailIfNoTests=false是另外一種路過 測試的方法
4.包含與排除測試用例
如果由於歷史原因,
測試類不符合默認的三種命名模式,可以通過pom.xml設置maven-surefire-plugin插件添加命名模式或排除一些命名模式。
- < plugin >
- < groupId > org.apache.maven.plugins </ groupId >
- < artifactId > maven-surefire-plugin </ artifactId >
- < version > 2.5 </ version >
- < configuration >
- < includes >
- < include > **/*Tests.java </ include >
- </ includes >
- < excludes >
- < exclude > **/*ServiceTest.java </ exclude >
- < exclude > **/TempDaoTest.java </ exclude >
- </ excludes >
- </ configuration >
- </ plugin >
5.生成測試報告
5.1基本測試報告
默認情況下,maven-surefire-plugin會在項目的target/surefire-reports目錄下生成兩種格式的錯誤報告。
- 簡單文本格式——內容十分簡單,可以看出哪個測試項出錯。
- 與JUnit兼容的XML格式——XML格式已經成為了Java單元測試報告的事實標准,這個文件可以用其他的工具如IDE來查看。
5.2測試覆蓋率報告
測試覆蓋率是衡量項目代碼質量的一個重要的參考指標。Cobertura是一個優秀的開源
測試覆蓋率統計工具(詳見 http://cobertura.sourceforge.net/),Maven通過cobertura-maven-plugin與之集成,用戶可 以使用簡單的命令為Maven項目生成
測試覆蓋率報告。運行下面命令生成報告:
- mvn cobertura:cobertura
6.運行TestNG測試
TestNG是Java社區中除了JUnit之外另一個流行的
單元
測試框架。TestNG在JUnit的基礎上增加了很多特性,其站點是http://testng.org/ .添加TestNG依賴:
- < dependency >
- < groupId > org.testng </ groupId >
- < artifactId > testng </ artifactId >
- < version > 5.9 </ version >
- < scope > test </ scope >
- < classifier > jdk15 </ classifier >
- </ dependency >
下面是JUnit和TestNG的常用類庫對應關系
JUnit類 | TestNG類 | 作用 |
org.junit.Test | org.testng.annotations.Test | 標注方法為測試方法 |
org.junit.Assert | org.testng.Assert | 檢查測試結果 |
org.junit.Before | org.testng.annotations.BeforeMethod | 標注方法在每個測試方法之前運行 |
org.junit.After | org.testng.annotations.AfterMethod | 標注方法在每個測試方法之后運行 |
org.junit.BeforeClass | org.testng.annotations.BeforeClass | 標注方法在所有測試方法之前運行 |
org.junit.AfterClass | org.testng.annotations.AfterClass | 標注方法在所有測試方法之后運行 |
TestNG允許用戶使用一個名為testng.xml的文件來配置想要運行的
測試集合。如在類路徑上添加testng.xml文件,配置只運行RandomGeneratorTest
- <? xml version = "1.0" encoding = "UTF-8" ?>
- < suite name = "Suite1" verbose = "1" >
- < test name = "Regression1" >
- < classes >
- < class name = "com.juvenxu.mvnbook.account.captcha.RandomGeneratorTest" />
- </ classes >
- </ test >
- </ suite >
同時再配置maven-surefire-plugin使用該testng.xml,如:
- < plugin >
- < groupId > org.apache.maven.plugins </ groupId >
- < artifactId > maven-surefire-plugin </ artifactId >
- < version > 2.5 </ version >
- < configuration >
- < suiteXmlFiles >
- < suiteXmlFile > testng.xml </ suiteXmlFile >
- </ suiteXmlFiles >
- </ configuration >
- </ plugin >
TestNG較JUnit的一大優勢在於它支持
測試組的概念。如可以在方法級別聲明
測試組:
- @Test (groups={ "util" , "medium" })
然后可以在pom中配置運行一個或多個
測試組:
- < plugin >
- < groupId > org.apache.maven.plugins </ groupId >
- < artifactId > maven-surefire-plugin </ artifactId >
- < version > 2.5 </ version >
- < configuration >
- < groups > util,medium </ groups >
- </ configuration >
- </ plugin >
7.重用測試代碼
當命令行運行mvn package的時候,Maven只會打包主代碼及資源文件,並不會對
測試代碼打包。如果
測試代碼中有需要重用的代碼,這時候就需要對
測試代碼打包了。
這時候需要配置maven-jar-plugin將
測試類打包,如:
maven-jar-plugin有兩個目標,分別為jar和test-jar。這兩個目標都默認綁定到default生命周期的package階段運行,只是test-jar並沒有在超級POM中配置,因此需要我們另外在pom中配置。
- < plugin >
- < groupId > org.apache.maven.plugins </ groupId >
- < artifactId > maven-jar-plugin </ artifactId >
- < version > 2.2 </ version >
- < executions >
- < execution >
- < goals >
- < goal > test-jar </ goal >
- </ goals >
- </ execution >
- </ executions >
- </ plugin >
現在如要引用test-jar生成的
測試代碼包,可以如下配置:
- < dependency >
- < groupId > com.juvenxu.mvnbook.account </ groupId >
- < artifactId > account-captcha </ artifactId >
- < version > 1.0.0-SNAPSHOT </ version >
- < type > test-jar </ type >
- < scope > test </ scope >
- </ dependency >
轉自:http://www.verydemo.com/demo_c290_i17360.html