maven运行junit用例并生成报告maven-surefire-plugin,maven-antrun-extended-plugin

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

测试类如下:

 

[java] view plain copy
 
  1. package com.mmnn.test.testcase;  
  2.   
  3. import static org.junit.Assert.assertTrue;  
  4.   
  5. import org.junit.Test;  
  6.   
  7. public class Demo1Test   
  8. {  
  9.     @Test  
  10.     public void TestMth1() {  
  11.         assertTrue("msg : mth1 test test test test", true);  
  12.     }  
  13.       
  14.     @Test  
  15.     public void TestMth2() {  
  16.         assertTrue("msg : mth2 test test test test", true);  
  17.     }  
  18. }  
 

 

[java] view plain copy
 
  1. package com.mmnn.test.testcase;  
  2.   
  3. import static org.junit.Assert.assertTrue;  
  4.   
  5. import org.junit.Test;  
  6.   
  7. public class Demo2Test   
  8. {  
  9.     @Test  
  10.     public void TestMth3() {  
  11.         assertTrue("msg : mth3 test test test test", true);  
  12.     }  
  13.       
  14.     @Test  
  15.     public void TestMth4() {  
  16.         assertTrue("msg : mth4 test test test test", false);  
  17.     }  
  18. }  
[plain] view plain copy
 
  1. export JAVA_HOME=/opt/tools/jdk1.7.0_17  
  2.   
  3. cd /path/to/testproject/whichhasPomFile  
  4.   
  5. /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
 
  1. mvn -s C:\maven\conf\settings.xml clean -Dtest=TestClass surefire-report:report test  

 

[html] view plain copy
 
  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.   
  8.     <properties>  
  9.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  10.     </properties>  
  11.   
  12.     <build>  
  13.         <plugins>  
  14.             <plugin>  
  15.                 <groupId>org.apache.maven.plugins</groupId>  
  16.                 <artifactId>maven-compiler-plugin</artifactId>  
  17.                 <version>2.0.2</version>  
  18.                 <configuration>  
  19.                     <source>1.8</source>        <!-- //////////// -->  
  20.                     <target>1.8</target>  
  21.                 </configuration>  
  22.             </plugin>  
  23.             <plugin>  
  24.                 <artifactId>maven-surefire-plugin</artifactId>  
  25.                 <configuration>  
  26.                     <testFailureIgnore>true</testFailureIgnore<!-- //////////// -->  
  27.                     <includes>  
  28.                         <include>**/*Test.java</include>    <!-- //////////// -->  
  29.                     </includes>  
  30.                     <excludes>  
  31.                         <!-- -->  
  32.                     </excludes>  
  33.                 </configuration>  
  34.             </plugin>  
  35.         </plugins>  
  36.     </build>  
  37.   
  38.     <dependencies>  
  39.         <dependency>  
  40.             <groupId>junit</groupId>  
  41.             <artifactId>junit</artifactId>  
  42.             <version>4.5</version>  
  43.             <scope>test</scope>  
  44.         </dependency>  
  45.     </dependencies>  
  46.   
  47. </project>  


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

 

 

 

二、使用maven-antrun-extended-plugin

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

 

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

运行mvn test:

 


posted @ 2018-02-11 12:37  小学生II  阅读(3303)  评论(0编辑  收藏  举报