使用gradle从android测试代码覆盖到声纳

时间:2014-03-25 10:21:24

标签: android gradle sonarqube jacoco

我必须在我的Android测试中设置代码覆盖率报告,然后在声纳中发布它们。我已经读过没有可以做到的工具和插件。我正在使用gradle脚本,我尝试jacoco插件,cobertura,但没有结果。有什么方法可以解决它吗?我也试着像Gradle jacoco code coverage - Then publish/show in Jenkins

那样做

1 个答案:

答案 0 :(得分:1)

带有代码覆盖率和声纳的build.gradle示例

apply plugin: 'com.android.application'

    android {

        compileSdkVersion 20
        buildToolsVersion '20.0.0'
        defaultConfig {
            applicationId 'com.example.coverage'
            minSdkVersion 11
            targetSdkVersion 19
            versionCode 1
            versionName '1.0'
            testHandleProfiling true
            testFunctionalTest true

        }
        buildTypes {
            debug {
                testCoverageEnabled true
            }
            release {
                runProguard false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        compileOptions {

            apply plugin: 'sonar-runner'
        }

        packagingOptions {
            exclude 'META-INF/DEPENDENCIES.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/dependencies.txt'
            exclude 'META-INF/LGPL2.1'
            exclude 'META-INF/services/javax.annotation.processing.Processor'
            exclude 'LICENSE.txt'
        }
    }

    sonarRunner {

        sonarProperties {

            property "sonar.projectKey", "coverage-example"
            property "sonar.projectName", "Coverage Example"
            property "sonar.projectVersion", "1.0"

            property "sonar.sources", "src/main/java"
            property "sonar.binaries", "build"
            property "sonar.test", "src/androidTest/java"

            property "sonar.profile", "Sonar way"
            property "sonar.language", "java"
            property "sonar.sourceEncoding", "UTF-8"

            property "sonar.dynamicAnalysis", "reuseReports"

            property "sonar.junit.reportsPath", "build/outputs/reports/coverage/debug"
            property "sonar.cobertura.reportPath", "build/outputs/reports/coverage/debug/cobertura.xml"
            property "sonar.java.coveragePlugin", "cobertura"

            property "sonar.host.url", "http://localhost:9099"
        }
    }

现在运行

  

gradlew clean assembleDebug createDebugCoverageReport

现在,您将能够在此目录中看到包含代码覆盖率报告的xml

  

应用程序/生成/输出/报告/覆盖/调试/ report.xml将

现在在我们运行声纳任务之前,我们必须转换一个sonar可以读取的report.xml,它将在python脚本中使用这个,它执行将此文件转换为插件声纳cobertura可以使用的格式它

Link the script on github

python cover2cover.py app/build/outputs/reports/coverage/debug/report.xml src/main/java > app/build/outputs/reports/coverage/debug/cobertura.xml && cp app/build/outputs/androidTest-results/connected/* app/build/outputs/reports/coverage/debug/

现在运行

gradlew sonarRunner