Maven的单元测试插件maven-surefire-plugin详解

2年前 (2022) 程序员胖胖胖虎阿
175 0 0

文章目录

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

  @BeforeEach
  public void setUp() {
    hm = new HelloMaven();
  }

  @Test
  public void testAdd() throws InterruptedException {
    int a = 1;
    int b = 2;
    int result = hm.add(a, b);
    Assert.assertEquals(a + b, result);
  }

  @Test
  public void testSubtract() throws InterruptedException {
    int a = 1;
    int b = 2;
    int result = hm.subtract(a, b);
    Assert.assertEquals(a - b, result);
  }

  @AfterEach
  public 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的单元测试插件maven-surefire-plugin详解
第三种:Maven Goal 的窗口中执行
Maven的单元测试插件maven-surefire-plugin详解
第四种:IDEA内置的命令终端窗口
Maven的单元测试插件maven-surefire-plugin详解

这里要特别注意,如果只是执行命令 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

版权声明:程序员胖胖胖虎阿 发表于 2022年11月8日 下午6:24。
转载请注明:Maven的单元测试插件maven-surefire-plugin详解 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...