zoukankan      html  css  js  c++  java
  • 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:mavenconfsettings.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:

  • 相关阅读:
    20210603
    20210602
    20210601
    20210531-已编辑
    2021053101
    操作系统笔记(一)
    尘埃落定,扬帆起航
    RTL级低功耗设计
    关于毛刺
    电路级拾珍
  • 原文地址:https://www.cnblogs.com/111testing/p/9060236.html
Copyright © 2011-2022 走看看