简体   繁体   English

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

[英]Code coverage to sonar from android tests using gradle

i have to set up code coverage report on my android tests and then publish them in sonar. 我必须在我的Android测试中设置代码覆盖率报告,然后在声纳中发布它们。 I have read that there are no tools and plugins which can do it. 我已经读过没有可以做到的工具和插件。 I am using gradle scripts and i try jacoco plugin, cobertura, but no results. 我正在使用gradle脚本,我尝试jacoco插件,cobertura,但没有结果。 Is it any ways to resolve it? 有什么方法可以解决它吗? Also i tried to do like here Gradle jacoco code coverage - Then publish/show in Jenkins 此外,我试图像这里做Gradle jacoco代码覆盖 - 然后在Jenkins发布/显示

Example of build.gradle with code-coverage and sonar 具有代码覆盖率和声纳的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"
        }
    }

Now run 现在跑

gradlew clean assembleDebug createDebugCoverageReport gradlew clean assembleDebug createDebugCoverageReport

Now you will be able to see a xml containing the code coverage report in this directory 现在,您将能够在此目录中看到包含代码覆盖率报告的xml

app/build/outputs/reports/coverage/debug/report.xml 应用程序/编译/输出/报告/报道/调试/ report.xml将

Now before we run the sonar task, we have to convert the report.xml something that sonar can read, it will use this done in python script that performs the conversion of this file to the format that the plugin sonar cobertura can use it. 现在在我们运行声纳任务之前,我们必须转换一个sonar可以读取的report.xml,它将在python脚本中使用这个,它执行将此文件转换为插件声纳cobertura可以使用它的格式。

Link the script on github 链接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/

Now run 现在跑

gradlew sonarRunner

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM