Maven的单元测试插件maven-surefire-plugin详解
文章目錄
- pom.xml 的配置(注意事項,非常重要)
- 測試案例
- 執行測試命令
- surefire 插件配置
pom.xml 的配置(注意事項,非常重要)
1.必須引入 maven-surefire-plugin 插件,否則無法使用 Maven 的測試功能
2.maven-surefire-plugin 插件只支持 junit-jupiter-api 構件,不支持 junit 構件
所以在 pom.xml 文件關于測試的配置內容如下:
<dependencies><!-- 必須使用junit-jupiter-api構件,測試注解、斷言都源于此構件--><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.8.2</version><scope>test</scope></dependency></dependencies><build><pluginManagement><plugins><!-- 必須顯式的聲明測試插件,否則無法執行測試 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M5</version></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.9</source><target>1.9</target><encoding>UTF-8</encoding></configuration></plugin></plugins></pluginManagement></build>測試案例
如果要使用 Maven 的批量測試功能,只能把測試用例寫在 src/test/java 目錄下的源代碼文件中。
首先在 src/main/java 下創建一個簡單的被測試的類,類的代碼如下:
public class HelloMaven {public int add(int a, int b) {return a + b;}public int subtract(int a, int b) {return a - b;} }接著在 src/test/java 目錄下創建測試用例(即用于測試的類),代碼如下:
package com.example.demo02;import org.junit.Assert; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test;/*** description** @author liaowenxiong* @date 2022/1/28 08:18*/public class HelloMavenTest {private HelloMaven hm;@BeforeEachpublic void setUp() {hm = new HelloMaven();}@Testpublic void testAdd() throws InterruptedException {int a = 1;int b = 2;int result = hm.add(a, b);Assert.assertEquals(a + b, result);}@Testpublic void testSubtract() throws InterruptedException {int a = 1;int b = 2;int result = hm.subtract(a, b);Assert.assertEquals(a - b, result);}@AfterEachpublic void tearDown() throws Exception {System.out.println("測試結束了!");} }執行測試命令
測試用例寫好之后,就要執行測試的命令,可以通過以下幾種方式執行測試的命令。
第一種:命令終端
打開命令終端,切換到 pom.xml 所在目錄下,執行命令 mvn test。執行命令 mvn test,表示執行到構件生命周期的 test 階段,之前的階段都會自動執行。執行 mvn test 命令的過程如下:
[~/Documents/IdeaProjects/struts2-demo01]$ mvn test [INFO] Scanning for projects... [INFO] [INFO] ------------------< priv.lwx.struts2:struts2-demo01 >------------------- [INFO] Building struts2-demo01 Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ struts2-demo01 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 2 resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ struts2-demo01 --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ struts2-demo01 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/liaowenxiong/Documents/IdeaProjects/struts2-demo01/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ struts2-demo01 --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ struts2-demo01 --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running priv.lwx.struts2.util.ConnectionUtilsTest 配置文件路徑:/Users/liaowenxiong/Documents/IdeaProjects/struts2-demo01/target/classes/db_config.properties Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. com.mysql.cj.jdbc.ConnectionImpl@221a3fa4 [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.954 s - in priv.lwx.struts2.util.ConnectionUtilsTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.345 s [INFO] Finished at: 2022-02-07T13:00:50+08:00 [I;NFO] ------------------------------------------------------------------------第二種:Maven 操作窗口
選擇生命周期的 test 階段,點擊上面的綠色三角圖標。
第三種:Maven Goal 的窗口中執行
第四種:IDEA內置的命令終端窗口
這里要特別注意,如果只是執行命令 mvn surefire:test,那么之前的生命周期階段是不會自動執行的,也就是說主代碼不會被構建,測試代碼也不會被構建,而是執行測試的指令,因為執行命令 mvn surefire:test 表示只執行 surefire 插件的 test 目標而已,執行命令過程如下:
[~/Documents/IdeaProjects/struts2-demo01]$ mvn surefire:test [INFO] Scanning for projects... [INFO] [INFO] ------------------< priv.lwx.struts2:struts2-demo01 >------------------- [INFO] Building struts2-demo01 Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-cli) @ struts2-demo01 --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running priv.lwx.struts2.util.ConnectionUtilsTest 配置文件路徑:/Users/liaowenxiong/Documents/IdeaProjects/struts2-demo01/target/classes/db_config.properties Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. com.mysql.cj.jdbc.ConnectionImpl@221a3fa4 [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.855 s - in priv.lwx.struts2.util.ConnectionUtilsTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.477 s [INFO] Finished at: 2022-02-07T12:54:54+08:00 [INFO] ------------------------------------------------------------------------surefire 插件配置
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M5</version><configuration><excludes><!-- 測試時排除指定的文件 --><exclude>**/TestConstants.java</exclude></excludes><!-- Maven運行測試用例時,是通過調用maven的surefire插件并fork一個子進程來執行用例的。forkmode屬性中指明是要為每個測試創建一個進程,還是所有測試在同一個進程中完成。 forkMode 可設置值有 “never”, “once”, “always” 和 “pertest”。 never:不創建子進程once:在一個進程中進行所有測試。once為默認設置,在Hudson上持續回歸時建議使用默認設置。 pretest: 每一個測試(測試用例/測試類)創建一個新進程,為每個測試創建新的JVM是單獨測試的最徹底方式,但也是最慢的,不適合hudson上持續回歸 always:在一個進程中并行的運行腳本(即測試方法),Junit4.7以上版本才可以使用,surefire的版本要在2.6以上提供這個功能,其中 threadCount:執行時,指定可分配的線程數量。只和參數parallel配合使用有效。默認:5。 --><forkMode>always</forkMode><parallel>methods</parallel><threadCount>4</threadCount></configuration> </plugin>補充:
The parameter forkMode is deprecated since version 2.14. Use forkCount and reuseForks instead.
在 2.14 版本之后,forkMode 不再使用,改成 forkCount 或者 reuseForks。
forkCount:選項指定并行分叉以執行測試的VM數量。當以“C”終止時,數字部分乘以CPU核數。浮點值只能與“C”一起接受。如果設置為“0”,則不會分叉任何VM,所有測試都在主進程內執行。
<forkCount>4</forkCount> <forkCount>1.5C</forkCount>reuseForks:指示是否可以重用分叉的VM。如果設置為“false”,則為每個要執行的測試類派生一個新的VM。如果設置為“true”,則最多會對forkCount虛擬機進行分叉,然后重新使用以執行所有測試。
<reuseForks>true</reuseForks> <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M5</version><configuration><reuseForks>true</reuseForks><forkCount>4</forkCount></configuration> </plugin>參見:https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#forkCount
總結
以上是生活随笔為你收集整理的Maven的单元测试插件maven-surefire-plugin详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 油纸可以放烤箱吗 油纸可不可以放烤箱
- 下一篇: 警告提示:No archetype fo