1. 程式人生 > >關於java專案單元測試報告的總結

關於java專案單元測試報告的總結

#背景 由於公司需要制定一份單元測試報告的規範,於是研究了幾個生成報告的maven外掛,特此記錄一下 #生成報告外掛maven-surefire-plugin 1、此外掛是最簡單也最簡陋的一個生成報告外掛,它可以生成html、xml、txt型別的報告 2、配置 org.apache.maven.plugins maven-surefire-plugin 2.22.1 true 3、執行命令 mvn test命令 會生成xml、txt格式的報告檔案,在target/surefire-report資料夾下,如果想要生成html格式的報告,需要執行命令mvn surefire-report:report命令,它會在target/site/目錄下生成surefire-report.html檔案 在這裡插入圖片描述

#生成報告外掛maven-antrun-extended-plugin 這個外掛能夠提供更友好的html介面,可讀性會比maven-surefire-plugin強一些 org.jvnet.maven-antrun-extended-plugin maven-antrun-extended-plugin test-reports test run org.apache.ant ant-junit 1.8.0 org.apache.ant ant-trax 1.8.0

	執行mvn test命令後,就會在target/antrun-reports下生成html格式的報告
	![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20181112150227132.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbmNhbzk1Mg==,size_16,color_FFFFFF,t_70)


	#使用外掛cobertura-maven-plugin生成覆蓋率報告
	  <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.5.1</version>
        </plugin>
        執行命令 mvn cobertura:cobertura命令生成報告
        ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20181112150444193.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbmNhbzk1Mg==,size_16,color_FFFFFF,t_70)
	
	#使用外掛jacoco-maven-plugin生成覆蓋率報告
		<plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.8</version>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>target/jacoco.exec</dataFile>
                        <outputDirectory>target/jacoco-report</outputDirectory>
                    </configuration>
                </execution>
            </executions>

            <!-- Configuration 裡面寫配置資訊 -->
            <configuration>

                <!-- rules裡面指定覆蓋規則 -->
                <rules>
                    <rule implementation="org.jacoco.maven.RuleConfiguration">
                        <element>BUNDLE</element>
                        <limits>
                            <!-- 指定方法覆蓋到80% -->
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>METHOD</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.80</minimum>
                            </limit>
                            <!-- 指定指令覆蓋到80% -->
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>INSTRUCTION</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.80</minimum>
                            </limit>
                            <!-- 指定行覆蓋到80% -->
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>LINE</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.80</minimum>
                            </limit>
                            <!-- 指定類覆蓋到100%,不能遺失任何類 -->
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>CLASS</counter>
                                <value>MISSEDCOUNT</value>
                                <maximum>0</maximum>
                            </limit>

                        </limits>
                    </rule>
                </rules>
            </configuration>
        </plugin>
        執行命令 mvn test 就會生成html格式的覆蓋率報告
        ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20181112150639932.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbmNhbzk1Mg==,size_16,color_FFFFFF,t_70)