【问题标题】:Sonar Jacoco not considering Kotlin androidTest (integration test case) in coverageSonar Jacoco 没有在覆盖范围内考虑 Kotlin androidTest(集成测试用例)
【发布时间】:2022-01-06 02:11:09
【问题描述】:

我为我的 android 项目编写了本地和集成测试用例。使用Kotlin(1.4.21)Robolectric(4.5.1)、sonar(2.7.1)、Jacoco(maven插件0.8.2)

问题是SonarJacoco没有考虑androidTest(集成测试用例)写在Kotlin的代码覆盖率 然而,声纳显示了其他测试用例的正确覆盖率 -

java 单元测试用例 -> 工作

koltin 单元测试用例 -> 工作

java 集成测试用例 -> 工作

kotlin 集成测试用例 -> 不工作

虽然我检查了我为声纳设置的路径,但都是正确的。

properties['sonar.java.binaries'] = files("${buildDir}/intermediates/javac/universalDebug/classes")
properties["sonar.java.binaries"] += files("${buildDir}/tmp/kotlin-classes/universalDebug/")

properties['sonar.java.test.binaries'] = files("${buildDir}/intermediates/javac/universalDebugAndroidTest/classes")
properties['sonar.java.test.binaries'] += files("${buildDir}/tmp/kotlin-classes/universalDebugAndroidTest/")

我已经解决了其他 stackoverflow 问题,但没有发现同样的问题。因此,我无法找出声纳没有显示我用 Kotlin 编写的集成测试用例的覆盖率的问题。

提前致谢

更新

在 adroidTest 文件夹中 > 我还有 2 个包。

MyApplicationTest> src> com > pkgA
                            > pkgB

它正在考虑 pkgA 中存在的测试文件,而不是其他文件。我最近创建了这个 pkgB 这可能是什么原因?我是否在某处更新了某些路径?

【问题讨论】:

    标签: android kotlin sonarqube integration-testing jacoco


    【解决方案1】:

    您可能需要执行以下操作

    tasks.withType(Test) {
        jacoco.includeNoLocationClasses = true
    }
    

    请注意,Java 11 的一些问题可能会导致您的测试失败,因此您可能还想排除 jdk.internal,如下所示

    tasks.withType(Test) {
        jacoco.includeNoLocationClasses = true
        excludes = ['jdk.internal.*']
    }
    

    或者有点冗长但有效的选项:

    subprojects {
        pluginManager.withPlugin("com.android.library"){
            android.testOptions.unitTests.all {
                jacoco {
                    includeNoLocationClasses = true
                    excludes = ['jdk.internal.*']
                }
            }
        }
        pluginManager.withPlugin("com.android.application"){
            android.testOptions.unitTests.all {
                jacoco {
                    includeNoLocationClasses = true
                    excludes = ['jdk.internal.*']
                }
            }
        }
        apply plugin: 'jacoco'
    }
    

    如果可能的话,我建议你也升级你的 jacoco 和 sonar 插件版本

    【讨论】:

    • 我已经更新了我的问题,请您检查一下并在这里提供帮助
    • 这种情况还在发生吗?如果是,你能分享一个有这个问题的项目吗?
    • 是的,但我无法共享该项目。您能否检查一下我的问题中的更新部分,即当前状态。当我将 pkgB 文件移动到 pgA 时,它会显示这些文件的覆盖范围
    猜你喜欢
    • 2012-04-14
    • 2016-01-16
    • 1970-01-01
    • 2017-10-01
    • 2014-01-21
    • 2018-07-08
    • 2013-03-16
    • 2017-06-19
    • 2018-07-24
    相关资源
    最近更新 更多