转载:http://blog.csdn.net/hdyrz/article/details/78398964

测试类如下:

  1. package com.mmnn.test.testcase;
  2. import static org.junit.Assert.assertTrue;
  3. import org.junit.Test;
  4. public class Demo1Test
  5. {
  6. @Test
  7. public void TestMth1() {
  8. assertTrue("msg : mth1 test test test test", true);
  9. }
  10. @Test
  11. public void TestMth2() {
  12. assertTrue("msg : mth2 test test test test", true);
  13. }
  14. }
 
  1. package com.mmnn.test.testcase;
  2. import static org.junit.Assert.assertTrue;
  3. import org.junit.Test;
  4. public class Demo2Test
  5. {
  6. @Test
  7. public void TestMth3() {
  8. assertTrue("msg : mth3 test test test test", true);
  9. }
  10. @Test
  11. public void TestMth4() {
  12. assertTrue("msg : mth4 test test test test", false);
  13. }
  14. }
  1. export JAVA_HOME=/opt/tools/jdk1.7.0_17
  2. cd /path/to/testproject/whichhasPomFile
  3. /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

  1. mvn -s C:\maven\conf\settings.xml clean -Dtest=TestClass surefire-report:report test
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.mmnn.test</groupId>
  5. <artifactId>mvnjunit1</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <properties>
  8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  9. </properties>
  10. <build>
  11. <plugins>
  12. <plugin>
  13. <groupId>org.apache.maven.plugins</groupId>
  14. <artifactId>maven-compiler-plugin</artifactId>
  15. <version>2.0.2</version>
  16. <configuration>
  17. <source>1.8</source>        <!-- //////////// -->
  18. <target>1.8</target>
  19. </configuration>
  20. </plugin>
  21. <plugin>
  22. <artifactId>maven-surefire-plugin</artifactId>
  23. <configuration>
  24. <testFailureIgnore>true</testFailureIgnore> <!-- //////////// -->
  25. <includes>
  26. <include>**/*Test.java</include>    <!-- //////////// -->
  27. </includes>
  28. <excludes>
  29. <!-- -->
  30. </excludes>
  31. </configuration>
  32. </plugin>
  33. </plugins>
  34. </build>
  35. <dependencies>
  36. <dependency>
  37. <groupId>junit</groupId>
  38. <artifactId>junit</artifactId>
  39. <version>4.5</version>
  40. <scope>test</scope>
  41. </dependency>
  42. </dependencies>
  43. </project>

运行 mvn test surefire-report:report 即可:

二、使用maven-antrun-extended-plugin

使用maven-surefire-plugin生成的报告太丑,可以通过maven-antrun系列插件生成更友好的报告:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.mmnn.test</groupId>
  5. <artifactId>mvnjunit2</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <properties>
  8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  9. </properties>
  10. <build>
  11. <plugins>
  12. <plugin>
  13. <groupId>org.apache.maven.plugins</groupId>
  14. <artifactId>maven-compiler-plugin</artifactId>
  15. <version>2.0.2</version>
  16. <configuration>
  17. <source>1.8</source>
  18. <target>1.8</target>
  19. </configuration>
  20. </plugin>
  21. <!-- mvn test生成xml txt测试报告(命令行不带surefire-report:report时) -->
  22. <plugin>
  23. <artifactId>maven-surefire-plugin</artifactId>
  24. <configuration>
  25. <testFailureIgnore>true</testFailureIgnore> <!-- //////////// -->
  26. <includes>
  27. <include>**/*Test.java</include>    <!-- //////////// -->
  28. </includes>
  29. <excludes>
  30. <!-- -->
  31. </excludes>
  32. </configuration>
  33. </plugin>
  34. <!-- 用mvn ant生成格式更友好的report -->
  35. <plugin>
  36. <groupId>org.jvnet.maven-antrun-extended-plugin</groupId>
  37. <artifactId>maven-antrun-extended-plugin</artifactId>   <!-- //////////// -->
  38. <executions>
  39. <execution>
  40. <id>test-reports</id>
  41. <phase>test</phase> <!-- //////////// -->
  42. <configuration>
  43. <tasks>
  44. <junitreport todir="${basedir}/target/surefire-reports">
  45. <fileset dir="${basedir}/target/surefire-reports">
  46. <include name="**/*.xml" />
  47. </fileset>
  48. <report format="frames" todir="${basedir}/target/surefire-reports" /> <!-- //////////// -->
  49. </junitreport>
  50. </tasks>
  51. </configuration>
  52. <goals>
  53. <goal>run</goal>
  54. </goals>
  55. </execution>
  56. </executions>
  57. <dependencies>
  58. <dependency>
  59. <groupId>org.apache.ant</groupId>
  60. <artifactId>ant-junit</artifactId>
  61. <version>1.8.0</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.apache.ant</groupId>
  65. <artifactId>ant-trax</artifactId>
  66. <version>1.8.0</version>
  67. </dependency>
  68. </dependencies>
  69. </plugin>
  70. </plugins>
  71. </build>
  72. <dependencies>
  73. <dependency>
  74. <groupId>junit</groupId>
  75. <artifactId>junit</artifactId>
  76. <version>4.5</version>
  77. <scope>test</scope>
  78. </dependency>
  79. </dependencies>
  80. </project>

运行mvn test:


最新文章

  1. iOS实现简书的账号识别方式(正则表达式)
  2. ecshop教程:重置后台密码MD5+salt
  3. 如何从MySQL官方Yum仓库安装MySQL5.6
  4. HashCode作用
  5. POJ 1716
  6. jquery仿alert提示框、confirm确认对话框、prompt带输入的提示框插件[附实例演示]
  7. Git学习01 --git add, git commit , git log ,git status, git reset --hard, head
  8. mysql 创建函数set global log_bin_trust_function_creators=TRUE;
  9. 随笔-SQL的三种存储引擎即三种类型的表
  10. linux-2.6.18源码分析笔记---信号
  11. UNIX环境高级编程——线程同步之读写锁以及属性
  12. Java的类加载器种类(双亲委派)
  13. git 三步走
  14. 统计分析与R软件-chapter2-5
  15. Codeforces 1076F Summer Practice Report dp
  16. 【T02】理解子网和CIDR的概念
  17. 【nginx,apache】thinkphp ,laravel,yii2开发运行环境搭建
  18. [剑指Offer]48-最长不含重复字符的子字符串(递归思想,循环实现)
  19. 用pyenv和virtualenv搭建单机多版本python虚拟开发环境
  20. jquery easyUI 时间格式

热门文章

  1. jQuery 入门笔记1
  2. Appium+python自动化28-name定位【转载】
  3. NetBeans中从实体创建Restful webservice工程
  4. 服务器老是出现502 Bad Gateway?
  5. 洛谷 P1177 【模板】快速排序 【快速排序/multiset排序】
  6. HDU 1427 速算24点【数值型DFS】
  7. ASP.NET Core 2.2 基础知识(八) 主机 (未完待续)
  8. java中的3大特性之继承
  9. Android的日志工具Log
  10. [BZOJ4772]显而易见的数论(数论)