maven运行junit用例并生成报告
原文地址https://blog.csdn.net/hdyrz/article/details/78398964
测试类如下:
[java] view plain copy
package com.mmnn.test.testcase;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class Demo1Test
{
@Test
public void TestMth1() {
assertTrue("msg : mth1 test test test test", true);
}
@Test
public void TestMth2() {
assertTrue("msg : mth2 test test test test", true);
}
}
[java] view plain copy
package com.mmnn.test.testcase;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class Demo2Test
{
@Test
public void TestMth3() {
assertTrue("msg : mth3 test test test test", true);
}
@Test
public void TestMth4() {
assertTrue("msg : mth4 test test test test", false);
}
}
[plain] view plain copy
export JAVA_HOME=/opt/tools/jdk1.7.0_17
cd /path/to/testproject/whichhasPomFile
/opt/tools/apache-maven-3.1.1/bin/mvn -s /opt/tools/apache-maven-3.1.1/conf/settings.xml clean -Dtest=TestCal test
一、使用maven-surefire-plugin插件自带report功能
注意:
1、单独运行mvn test,默认执行的就是maven-surefire-plugin
2、配置了maven-surefire-plugin后,运行mvn test,结果和1一致,生成xml、txt文件
3、运行mvn test surefire-report:report 即是:运行mvn test的基础上,根据生成的xml、txt文件,再生成默认格式的report
[plain] view plain copy
mvn -s C:\maven\conf\settings.xml clean -Dtest=TestClass surefire-report:report test
[html] view plain copy
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mmnn.test</groupId>
<artifactId>mvnjunit1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.8</source> <!-- //////////// -->
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore> <!-- //////////// -->
<includes>
<include>**/*Test.java</include> <!-- //////////// -->
</includes>
<excludes>
<!-- -->
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
运行 mvn test surefire-report:report 即可:
二、使用maven-antrun-extended-plugin
使用maven-surefire-plugin生成的报告太丑,可以通过maven-antrun系列插件生成更友好的报告:
[html] view plain copy
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mmnn.test</groupId>
<artifactId>mvnjunit2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- mvn test生成xml txt测试报告(命令行不带surefire-report:report时) -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore> <!-- //////////// -->
<includes>
<include>**/*Test.java</include> <!-- //////////// -->
</includes>
<excludes>
<!-- -->
</excludes>
</configuration>
</plugin>
<!-- 用mvn ant生成格式更友好的report -->
<plugin>
<groupId>org.jvnet.maven-antrun-extended-plugin</groupId>
<artifactId>maven-antrun-extended-plugin</artifactId> <!-- //////////// -->
<executions>
<execution>
<id>test-reports</id>
<phase>test</phase> <!-- //////////// -->
<configuration>
<tasks>
<junitreport todir="${basedir}/target/surefire-reports">
<fileset dir="${basedir}/target/surefire-reports">
<include name="**/*.xml" />
</fileset>
<report format="frames" todir="${basedir}/target/surefire-reports" /> <!-- //////////// -->
</junitreport>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-junit</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-trax</artifactId>
<version>1.8.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
运行mvn test:
最新文章
- 【流量劫持】沉默中的狂怒 —— Cookie 大喷发
- 【小白的CFD之旅】15 四种境界
- 初学C#和MVC的一些心得,弯路,总结,还有教训(1)--语言的选择
- 从偶然的机会发现一个mysql特性到wooyun waf绕过题
- CAS 单点登录流程
- zabbix 分布式监控(proxy)源码安装
- mybatis 复习笔记03
- Android invalidate() 和 postInvalidate()的区别
- Thrift原理与使用实例
- Vijos P1062 迎春舞会之交谊舞
- jMeter之二
- Net Core Docker
- PS之火焰铁锈字
- Object-C非正式协议与正式协议的区别
- mysql 查询某字段里含有(或者不含)某字符的所有记录方法(转)
- 对于ios7扫描二维码功能的实现
- 在SSRS的每一页重复显示table header
- springboot小技巧(转)
- 针对微信的一篇推送附有的数据链接进行MapReduce统计
- luogu P4146 序列终结者
热门文章
- Android 读写位于SD卡上的sqlite数据库文件错误问题
- pl/sql编程2-综合
- PyQt4将窗口放在屏幕中间
- 《C#高级编程》学习笔记------什么是COM组件
- intellij idea 2018注册码|intellij idea 2018破解文件下载(附破解教程/汉化包)
- JavaScript DOM 对象
- LeetCode——Basic Calculator II
- Python 中的线程-进程1
- c# SQL Server数据库操作-数据适配器类:SqlDataAdapter
- 170628、springboot编程之Druid数据源和监控配置一