1. 在Maven中配置測試插件surefire
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
2. 默認被執行的測試
默認情況下,surefire會執行文件名以Test開頭或結尾的測試用例,或者是以TestCase結尾的測試用例。
3. 跳過測試 Skipping Tests
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<skipTests>true</skipTests>
</configuration></span>
</plugin>
</plugins>
</build>
補充:如果使用Junit或者TestNG也可以直接在當前測試方法上加註解@Ignore忽略,這是加了該註解的也都會被skip
4. 排除測試 Exclusions (Junit & TestNG 通用)
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<excludes>
<exclude>**/TestCircle.java</exclude>
<exclude>**/TestSquare.java</exclude>
</excludes></span>
</configuration>
</plugin>
</plugins>
</build>
5. 僅執行一個/一類測試(repeat) Running a Single Test (Junit & TestNG 通用)
在開發過程中配置命令:
mvn -Dtest=TestCircle test test表示當前測試方法所在的測試類,不需要擴展名 The value for the
test
parameter is the name of the test class
mvn -Dtest=TestCi*le test *表示任何
mvn -Dtest=TestSquare,TestCi*le test 如果測試類沒有使用規範的命名,可以顯示的直接指定測試方法的名稱
6. 如何使用TestNG
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.3.1</version>
<scope>test</scope>
</dependency>
If using an older version of TestNG (<= 5.11)
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.11</version>
<scope>test</scope>
<classifier>jdk15</classifier></span>
</dependency>
默認會執行的測試用例:*Test.java
7. 如何使用TestNG的 suite
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles></span>
</configuration>
</plugin>
8. 執行羣組測試 execute one or more specific groups (Junit & TestNG 通用)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<groups>functest,perftest</groups>
</configuration>
</plugin>
9. 多線程的運行測試用例 Running tests in parallel (Junit & TestNG 通用)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<parallel>methods</parallel>
<threadCount>10</threadCount>
</configuration>
</plugin>
10. 如何使用Junit Using JUnit
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
11. 如何使用Junit Category Using JUnit Categories
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<configuration>
<span style="color:#009900;"> <strong><groups>com.mycompany.SlowTests</groups></strong></span>
</configuration>
</plugin>
僅有帶該註解的測試 或者 是當前註解 類/接口的 子類 會被執行,
12. 如何debug TestCases
mvnDebug -DforkCount=0 test dubug非fork的測試
13. 使用系統屬性 Using System Properties
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<span style="color:#009900;"><systemPropertyVariables>
<propertyName>propertyValue</propertyName>
<buildDirectory>${project.build.directory}</buildDirectory>
[...]
</systemPropertyVariables></span>
</configuration>
</plugin>
</plugins>
</build>
14. 選擇surefire provider
surefire 默認會根據工程的classpath中已有的Junit|TestNG的版本來選擇 test-framework provider,我們也可以手動的選擇和覆蓋當前的provider
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<span style="color:#009900;"> <artifactId>surefire-junit47</artifactId></span>
<version>2.17</version>
</dependency>
</dependencies>
</plugin>
目前已經提供的provider有surefire-junit3, surefire-junit4, surefire-junit47 and surefire-testng
注意:選擇手動指定provider時,不要忘記安裝test framework