单元测试代码覆盖率

背景:项目开发中由于研发任务紧,开发人员往往不写单元测试或者随意编写几个测试方法,对于技术经理来说需要通过技术手段来强制要求开发人员编写单元测试,并且要求达到指定的测试覆盖率标准才能达到提测标准等要求。

项目以采用Maven 工程为例:

pom.xml 配置依赖和插件配置


    
    
            org.jacoco
            jacoco-maven-plugin
            0.7.8
            test
    



        
            
                org.jacoco
                jacoco-maven-plugin
                0.7.8
                
                    
                        prepare-agent
                        
                            prepare-agent
                        
                    
                    
                        check
                        
                            check
                        
                    
                    
                        report
                        prepare-package
                        
                            report
                        
                    
                
                
                    
                    
                        
                            BUNDLE
                            
                                
                                
                                    METHOD
                                    COVEREDRATIO
                                    0.8
                                
                                
                                
                                    INSTRUCTION
                                    COVEREDRATIO
                                    0.8
                                
                                
                                
                                    LINE
                                    COVEREDRATIO
                                    0.8
                                
                                
                                
                                    CLASS
                                    MISSEDCOUNT
                                    0
                                
                            
                        
                    
                
            
        
    

运行单元测试并统计代码覆盖率命令:

mvn clean compile jacoco:report test install

结果由于单元测试覆盖率不满足以上要求规则,出现一下错误。

[INFO] --- jacoco-maven-plugin:0.7.8:check (check) @ lyh-api ---
[INFO] Loading execution data file /Users/kevin/java_home/lyh-api/target/jacoco.exec
[INFO] Analyzed bundle 'lyh-api' with 12 classes
[WARNING] Rule violated for bundle lyh-api: methods covered ratio is 0.3, but expected minimum is 0.8
[WARNING] Rule violated for bundle lyh-api: instructions covered ratio is 0.2, but expected minimum is 0.8
[WARNING] Rule violated for bundle lyh-api: lines covered ratio is 0.3, but expected minimum is 0.8
[WARNING] Rule violated for bundle lyh-api: classes missed count is 4, but expected maximum is 0

此时要求开发人员技术补全单元测试,这里由于演示使用,可以将要求标准降低,使其满足要求并生成测试覆盖率报告

重新执行命令:mvn clean compile jacoco:report test install

不出意外测试成功,就可以在工程target/site/jacoco/index.html 查看测试报告

单元测试代码覆盖率_第1张图片

你可能感兴趣的:(java,单元测试,代码覆盖率,代码覆盖率)