天天看點

使用maven進行測試(九)

 maven本身并不是一個單元測試架構,java世界中主流的測試架構為junit和testng。maven所做的隻是在建構執行到特定生命周期階段的時候,通過插件來執行junit或者testng的測試用例。這一插件就是maven-surefire=plugin,可以稱之為測試運作器,它能很好的相容junit3、junit4和testng。

 maven的default生命周期test階段内置綁定上述插件的test目标,插件會執行特定目标下的符合命名格式的測試類,命名格式如下:

**/Test*.java:任何子目錄下所有命名以Test開頭的java類。

***TestCase.java:任何子目錄下所有命名以TestCase結尾的java類。

隻有符合這些标準的測試類才會得以執行。

當然,如果有需要,可以自己定義要運作測試類的模式,另外上述插件還支援更進階的testng測試集合xml檔案。

跳過測試運作

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中全局配置插件:

<plugin>

          <groupId>org.apache.maven.plugins</groupId>

          <artifactId>maven-surefire-plugin</artifactId>

          <version>2.5</version>

          <configuration>

            <sikp>true</sikp>

          </configuration>

</plugin>

<plugin>

          <groupId>org.apache.maven.plugins</groupId>

          <artifactId>maven-compiler-plugin</artifactId>

          <configuration>

            <skip>true</skip>

          </configuration>

</plugin>

動态指定要運作的測試用例:

mvn test -Dtest=abcTest   隻運作這個abcTest測試類

mvn test -Dtest=abc*Test   星号可以比對零個或者多個字元,表示運作以abc開頭Test結尾的所有測試類。

mvn test -Dtest=abcTest,defTest   隻運作這兩個測試類。

mvn test -Dtest 如果隻執行這個指令,插件找不到測試類的情況下會導緻建構失敗。

mvn test -Dtest -DfailIfNoTests=false 告訴插件,即使沒有任何測試類也不要報錯。

遺憾的是,在指令行不能指定某個測試類做排除測試。

包含與排除測試用例:

**是指比對任何路徑,*比對除路徑分隔符外的0個或多個字元。

include元素指定要執行的測試用例,exclude指定要排除的測試用例。

<plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-surefire-plugin</artifactId>

        <version>2.5</version>

          <configuration>

             <includes>

                <include>***Test.java</exclude>

             </excludes>

          </configuration>

</plugin>

測試報告的生成:maven-surefire-plugin生成測試報告

預設情況下,會在項目的target/surefite-reports目錄下生成兩種格式的錯誤報告:

1.簡單文本格式

2.與junit相容的xml格式,這個格式的報告可以用于三方工具的解析。

測試覆寫率報告:cobertura-maven-plugin,生成在target/site/cobertura目錄下。

在pom.xml目錄下運作 mvn cobertura:cobertura

運作TestNG測試

testNG測試的普通用法和junit測試是一樣的,另外TestNG支援使用者使用一個testng.xml的檔案來配置想要運作的測試集合,testng.xml檔案放在根目錄下,檔案内容如:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > 

<suite name="Suite1" verbose="1" > 

<groups>

<run>

<include name="..." />

<exclude name="..." /> 

</run>

</groups> 

<test name="Regression1"> 

 ...

</test>

</suite>

另外,配置插件cobertura-maven-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的一大優勢是支援測試組的概念:

<plugin>

          <groupId>org.apache.maven.plugins</groupId>

          <artifactId>maven-surefire-plugin</artifactId>

          <version>2.5</version>

          <configuration>

             <groups>util1,util2</groups>

          </configuration>

</plugin>

測試代碼可以重用,我們可以把測試代碼打包并部署到倉庫供其他項目使用:

<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>

當需要引用測試jar包的時候,坐标的類型用:<type>test-jar</type>

繼續閱讀