单元测试报告

简介

	最近有个项目需要我们能够出一份单元测试报告,以前都是写测试用例,直接运行查看结果,没有生成过测试报告,所以借这个机会研究了几个生成报告的插件

内容介绍

首先我们使用Juint编写测试代码,使用Maven构建项目,涉及到的插件包括
1、maven-surefire-plugin 生成报告的插件
2、maven-antrun-extended-plugin 生成比较友好的报告的插件
3、cobertura-maven-plugin 代码覆盖率插件
4、jacoco-maven-plugin 代码覆盖率插件

maven-surefire-plugin插件

配置


            org.apache.maven.plugins
            maven-surefire-plugin
            2.22.1
            
                
                true
                
               
            
            
        

执行命令

mvn surefire-report:report生成html格式的报告,报告格式如下
单元测试报告_第1张图片

maven-antrun-extended-plugin插件

maven-surefire-plugin生成的报告格式不够友好,阅读性比较差,使用maven-antrun-extended-plugin插件可以生成比较友好的html报告

配置

	 
            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 生成html文件,文件格式如下

	![这个格式相对于上面那种,友好度好多了](https://img-blog.csdnimg.cn/20181112152818341.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbmNhbzk1Mg==,size_16,color_FFFFFF,t_70)

cobertura-maven-plugin生成代码覆盖率报告

配置

	
            org.codehaus.mojo
            cobertura-maven-plugin
            2.5.1
        

##执行命令
mvn cobertura:cobertura
执行完成后会生成html格式的覆盖率报告,如下
单元测试报告_第2张图片

jacoco-maven-plugin代码覆盖率报告插件

配置

	
            org.jacoco
            jacoco-maven-plugin
            0.7.8
            
                
                    prepare-agent
                    
                        prepare-agent
                    
                
                
                    check
                    
                        check
                    
                
                
                    post-test
                    test
                    
                        report
                    
                    
                        target/jacoco.exec
                        target/jacoco-report
                    
                
            

            
            

                
                
                    
                        BUNDLE
                        
                            
                            
                                METHOD
                                COVEREDRATIO
                                0.80
                            
                            
                            
                                INSTRUCTION
                                COVEREDRATIO
                                0.80
                            
                            
                            
                                LINE
                                COVEREDRATIO
                                0.80
                            
                            
                            
                                CLASS
                                MISSEDCOUNT
                                0
                            

                        
                    
                
            
        

执行命令

	执行mvn test 后,会生成html格式的覆盖率报告,同时还会生成excel格式的报告

单元测试报告_第3张图片

总结

1、maven-surefire-plugin插件是基础的报告生成插件,可以生成xml、txt、html格式的报告(mvn  test命令只会生成xml、txt格式,执行mvn surefire-report:report才会生成html格式的报告)
2、maven-antrun-extended-plugin生成的html格式会更友好一些,更具可读性
3、cobertura-maven-plugin和jacoco-maven-plugin都可以生成html格式的覆盖率报告,jacoco-maven-plugin的配置更丰富,灵活性更强一些
4、其实后面的插件都是依赖maven-surefire-plugin插件而执行的
5、如果希望出现测试失败的时候也要生成报告,则设置maven-surefire-plugin: 	true即可

你可能感兴趣的:(单元测试报告)