【问题标题】:Android add unit test coverage into sonarAndroid将单元测试覆盖率添加到声纳中
【发布时间】:2016-05-24 23:54:24
【问题描述】:

我有一个由 Sonar 分析的 Android 项目。我看到了所有问题,但单元测试没有覆盖。我尝试了很多东西,但仍然无法正常工作。当我调用“gradlew createDebugCoverageReport”时,它会在 build/reports/coverage/debug 中创建目录,其中包含许多文件,例如 report.xml 和 index.html,其中包含测试覆盖率数据。但是在覆盖范围下的声纳中,我收到类似“此组件没有覆盖详细信息”的消息。我怎样才能让它工作?

命令序列:

gradlew clean assemble
gradlew createDebugCoverageReport
gradlew sonarRunner

构建.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "cz.meteocar.unit"
        minSdkVersion 16
        targetSdkVersion 20
        versionCode 3
        versionName "1.0.2"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            }
            debug {
                debuggable true
                testCoverageEnabled = true
            }


        }
    }


apply plugin: 'sonar-runner'

sonarRunner {
      sonarProperties {
        property "sonar.host.url", "http://localhost:9000" // Address of Sonar         server
        property "sonar.sources", "src/main/java" // Sources
        property "sonar.projectName", "Meteocar" // Name of your project
        property "sonar.projectVersion", "1.0.2" // Version of your project
        property "sonar.projectDescription", "Sonar" // Description of your project
        property "sonar.junit.reportsPath", "build/reports/coverage/"
    }
}



dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile files('libs/mbassador-1.2.0.jar')
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.android.support:support-v4:20.0.0'
    compile 'org.apache.commons:commons-lang3:3.0'
    compile 'com.jjoe64:graphview:4.0.1'
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.9.5'
    testCompile 'org.powermock:powermock-api-mockito:1.6.1'
    testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.1'
    testCompile 'org.powermock:powermock-module-junit4-rule:1.6.1'
    testCompile 'org.powermock:powermock-module-junit4:1.6.1'
    testCompile 'org.json:json:20140107'
}

【问题讨论】:

    标签: android unit-testing sonarqube android-gradle-plugin build.gradle


    【解决方案1】:

    您可以使用Jacoco 获取覆盖结果。它适用于sonar

    1.在SonarQube上安装Android插件

    2.添加jacoco plugin并在Gradle上创建gradle task以生成coverage文件,smth like:

    apply plugin: 'jacoco'
    
    jacoco {
        version "0.7.1.201405082137"
    }
    
    task jacocoTestReport(type: JacocoReport, dependsOn: "testDebugUnitTest") {
        group = "Reporting"
        description = "Generate Jacoco coverage reports after running tests."
        reports {
            xml.enabled = true
            html.enabled = true
        }
        classDirectories = fileTree(
                dir: 'build/intermediates/classes/debug/com/yourapppackagename',
                excludes: ['**/R.class',
                           '**/R$*.class',
                           '**/BuildConfig.*',
                           '**/Manifest*.*'])
        sourceDirectories = files('src/main/java')
        executionData = files('build/jacoco/testDebugUnitTest.exec')
        doFirst {
            files('build/intermediates/classes/debug').getFiles().each { file ->
                if (file.name.contains('$$')) {
                    file.renameTo(file.path.replace('$$', '$'))
                }
            }
        }
    }
    

    3.在你的根目录下创建sonar.properties文件并include覆盖文件路径:

    sonar.jacoco.reportPath=sdk/build/jacoco/testDebugUnitTest.exec
    sonar.junit.reportsPath=sdk/build/test-results/debug
    

    4.运行jacoco gradle task:

     ./gradlew clean assembleDebug jacocoTestReport
    

    5.运行sonar analyses

    【讨论】:

    • 如何扩展此解决方案,使其也适用于 Instrumentation 测试 (AndroidJUnit4)?最好是全部在一份报告中?
    • 那么你到底想让它工作什么?如果它关于通过或失败的测试声纳也支持它们,并且在 sonarqube 官方页面上有关于如何集成它的文档。它比添加覆盖率报告更容易,您只需定义测试代码的路径
    • 我想看到声纳上的联合测试和集成测试覆盖率以及一些整体覆盖率。整体覆盖率将由它自己从单元测试和集成测试中计算出来。我已经拥有单元测试的覆盖范围。现在我的问题是,我有来自 html 格式的仪器测试报告(对我来说是集成测试),我需要将其转换为 Jacoco 报告,以便我可以添加到声纳中。
    • 这是另一个问题,应该在SO 上单独打开。您必须详细解释您的问题并展示您迄今为止所做的尝试。
    • 坦克寻求帮助。我发现我的问题出在构建工具版本中。将其更改为 23.0.3 后一切正常。
    猜你喜欢
    • 2019-11-01
    • 2013-10-14
    • 1970-01-01
    • 2014-05-03
    • 2016-11-10
    • 2016-11-18
    • 2016-08-04
    • 1970-01-01
    • 2017-10-25
    相关资源
    最近更新 更多