您当前的位置: 首页 >  单元测试

liaowenxiong

暂无认证

  • 6浏览

    0关注

    1171博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

liaowenxiong 发布时间:2022-01-28 22:52:40 ,浏览量:6

文章目录
  • pom.xml 的配置(注意事项,非常重要)
  • 测试案例
  • 执行测试命令
  • surefire 插件配置

pom.xml 的配置(注意事项,非常重要)

1.必须引入 maven-surefire-plugin 插件,否则无法使用 Maven 的测试功能

2.maven-surefire-plugin 插件只支持 junit-jupiter-api 构件,不支持 junit 构件

所以在 pom.xml 文件关于测试的配置内容如下:


		
		
			org.junit.jupiter
			junit-jupiter-api
			5.8.2
			test
		
	
	
		
			
				
				
					org.apache.maven.plugins
					maven-surefire-plugin
					3.0.0-M5
				
				
					org.apache.maven.plugins
					maven-compiler-plugin
					
						1.9
						1.9
						UTF-8
					
				
			
		
	
测试案例

如果要使用 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] -------------------------------------
[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] -------------------------------------
[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 插件配置

  org.apache.maven.plugins
  maven-surefire-plugin
  3.0.0-M5
  
    
      
      **/TestConstants.java
    
    
    always
    methods
    4
  

补充: 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,所有测试都在主进程内执行。

4
1.5C

reuseForks:指示是否可以重用分叉的VM。如果设置为“false”,则为每个要执行的测试类派生一个新的VM。如果设置为“true”,则最多会对forkCount虚拟机进行分叉,然后重新使用以执行所有测试。

true 

	org.apache.maven.plugins
	maven-surefire-plugin
	3.0.0-M5
	
		true
		4
	

参见:https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#forkCount

关注
打赏
1661566967
查看更多评论
立即登录/注册

微信扫码登录

0.0464s