How to make Sonar ignore some classes for codeCoverage metric?

后端 未结 8 1927
花落未央
花落未央 2020-12-08 09:18

I have a Sonar profile in Maven. Everything works fine except the code coverage metric. I want to make Sonar ignore some classes only for the code coverage metric. I have th

相关标签:
8条回答
  • 2020-12-08 09:59

    Accordingly to this document for SonarQube 7.1

    sonar.exclusions - Comma-delimited list of file path patterns to be excluded from analysis sonar.coverage.exclusions - Comma-delimited list of file path patterns to be excluded from coverage calculations

    This document gives some examples on how to create path patterns

    # Exclude all classes ending by "Bean"
    # Matches org/sonar.api/MyBean.java, org/sonar/util/MyOtherBean.java, org/sonar/util/MyDTO.java, etc.
    sonar.exclusions=**/*Bean.java,**/*DTO.java
    
    # Exclude all classes in the "src/main/java/org/sonar" directory
    # Matches src/main/java/org/sonar/MyClass.java, src/main/java/org/sonar/MyOtherClass.java
    # But does not match src/main/java/org/sonar/util/MyClassUtil.java
    sonar.exclusions=src/main/java/org/sonar/*
    
    # Exclude all COBOL programs in the "bank" directory and its sub-directories
    # Matches bank/ZTR00021.cbl, bank/data/CBR00354.cbl, bank/data/REM012345.cob
    sonar.exclusions=bank/**/*
    
    # Exclude all COBOL programs in the "bank" directory and its sub-directories whose extension is .cbl
    # Matches bank/ZTR00021.cbl, bank/data/CBR00354.cbl
    sonar.exclusions=bank/**/*.cbl
    

    If you are using Maven for your project, Maven command line parameters can be passed like this for example -Dsonar.coverage.exclusions=**/config/*,**/model/*

    I was having problem with excluding single class explicitly. Below my observations:

    **/*GlobalExceptionhandler.java - not working for some reason, I was expecting such syntax should work
    com/some/package/name/GlobalExceptionhandler.java - not working
    src/main/java/com/some/package/name/GlobalExceptionhandler.java - good, class excluded explicitly without using wildcards
    
    0 讨论(0)
  • 2020-12-08 10:02

    At the time of this writing (which is with SonarQube 4.5.1), the correct property to set is sonar.coverage.exclusions, e.g.:

    <properties>
        <sonar.coverage.exclusions>foo/**/*,**/bar/*</sonar.coverage.exclusions>
    </properties>
    

    This seems to be a change from just a few versions earlier. Note that this excludes the given classes from coverage calculation only. All other metrics and issues are calculated.

    In order to find the property name for your version of SonarQube, you can try going to the General Settings section of your SonarQube instance and look for the Code Coverage item (in SonarQube 4.5.x, that's General Settings → Exclusions → Code Coverage). Below the input field, it gives the property name mentioned above ("Key: sonar.coverage.exclusions").

    0 讨论(0)
提交回复
热议问题