Maven测试并产生测试报告

首先需要有junit测试依赖包

   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

需要两个插件maven-surefire-plugin和maven-surefire-report-plugin
具体可以查看官方文档:https://maven.apache.org/plugins/index.html
在pom.xml文件中添加一下两个插件

<build>
	<plugins>
		<plugin>
			<artifactId>maven-surefire-plugin</artifactId>
			<!-- 2.19.1才开始支持指定方法的测试 -->
			<version>2.19.1</version>
			<!-- 跳过测试 -->
			<configuration>
				<skip>false</skip>
			</configuration>
		</plugin>
	</plugins>
</build>
<reporting>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-surefire-report-plugin</artifactId>
			<version>3.0.0-M4</version>
			<configuration>
				<showSuccess>false</showSuccess>
			</configuration>
		</plugin>
	</plugins>
</reporting>

测试方法:

跑指定的一个方法:
mvn surefire:test -Dtest=FacadeTradeService4FeeShareTest
跑指定的多个方法
mvn surefire:test -Dtest=FacadeTradeService4FeeShareTest,FacadeTradeServiceTest
字符匹配模式
mvn surefire:test -Dtest=FacadeTrade*Test
debug模式测试
mvn -Dmaven.surefire.debug test
仅仅报告失败测试
mvn surefire-report:report -DshowSuccess=false
在linux下将测试结果输出到指令文件(设置系统参数为oracle或mysql)
mvn clean test -D db=oracle > /usr/local/result/1.3.10.12.oracle.result.txt
mvn clean test -D db=mysql > /usr/local/result/1.3.10.12.mysql.result.txt

查看结果
在对应项目目录下target\surefire-reports可以查看测试结果
在这里插入图片描述
添加插件到 中

<reporting>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-surefire-report-plugin</artifactId>
			<version>3.0.0-M4</version>
			<configuration>
				<showSuccess>false</showSuccess>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-site-plugin</artifactId>
			<version>2.1</version>
		</plugin>
	</plugins>
</reporting>

mvn clean site -Dtest=QuotaLimitService4BondTest 指定测试
mvn clean site

发布了34 篇原创文章 · 获赞 1 · 访问量 1541

猜你喜欢

转载自blog.csdn.net/m0_37607945/article/details/104811663