android - sonarqube 具有 android 测试和测试覆盖率

标签 android unit-testing gradle sonarqube jacoco

我正在尝试在我的 Android 项目中设置 sonarqube 报告。我目前无法在 Sonar UI 中显示所有测试类,覆盖范围以百分比显示,目前仅来自 app/src/test/ 的单元测试显示为单元测试。

我的项目有一个测试文件夹app/src/test/其中包含单元测试,我有一个 androidTest 文件夹 app/src/androidTest/其中包含 Android 单元、集成和 UI 测试。当我通过 gradle 运行所有测试时,android-gradle 插件会生成 build/jacoco/testDebugUnitTest.execbuild/test-results/debug/TEST-*Test.xml其中包含测试文件夹中单元测试的 jacoco 结果和覆盖率报告。 android-gradle 插件还会生成 build/outputs/code-coverage/connected/coverage.ecbuild/outputs/androidTest-results/connected/TEST-*Test.xml包含 androidTest 文件夹中的结果和覆盖率报告

在我的 build.gradle 中,我可以指定 Sonar 插件的属性。

 sonarqube {
        properties {
            property "sonar.sources", "src/main/java,src/main/res"
            property "sonar.tests", "src/test/java,src/androidTest/java"

            property "sonar.java.coveragePlugin", "jacoco"
            property "sonar.jacoco.reportPath", "${project.buildDir}/jacoco/testDebugUnitTest.exec"
            property 'sonar.jacoco.itReportPath', "${project.buildDir}/outputs/code-coverage/connected/coverage.ec"

            property "sonar.junit.reportsPath", "${project.buildDir}/test-results/debug" // path to junit reports
        }
    }

sonar.junit.reportsPath我可以指定将哪个 xml 报告发送到 sonarqube 服务器。当我将其更改为build/outputs/androidTest-results/connected时我在仪表板上将 androidTest 显示为单元测试。有没有办法让 Sonar 插件在两个目录中查找或将结果合并在一起?

最佳答案

直到https://jira.sonarsource.com/browse/SONAR-4101已修复,您唯一的选择是编写一个任务,将测试结果文件复制到一个位置并将其配置为 sonar.junit.reportsPath,如下所示:

task combineTestResultsForSonarqube {
    group = "Reporting"
    def modules = ["app", "and", "other", "modules"];
    doLast {
        modules.each { module ->
            File combined = file("${module}/build/combined-test-results")
            if (combined.exists()) {
                combined.deleteDir()
            }
            combined.mkdirs();

            def testDirs = [file("${module}/build/test-results/debug/"),
                            file("${module}/build/outputs/androidTest-results/connected/")];
            testDirs.each { testDir ->
                if (!testDir.exists()) {
                    logging.captureStandardOutput LogLevel.WARN
                    println "WARNING: ignoring non-existant ${testDir.path}"
                    return;
                }
                files(testDir.listFiles()).each { file ->
                    new File(combined, file.getName()) << file.text
                }
            }
        }
    }
}

当您的构建中有风格时,路径当然必须进行调整。

关于android - sonarqube 具有 android 测试和测试覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37280289/

相关文章:

java - 将任意流连接到标准输入

android - javax.net.ssl.SSLHandshakeException : java. lang.IllegalArgumentException : Invalid input to toASCII: ip_nbae7bac35. kodrive.xyz

java - 在 Windows 7 中创建新的 JFileChooser 作为本地系统帐户时 Win32ShellFolder2.access 中的 NPE

android - 覆盖 Android 中的 SSL 信任管理器

python - 使用 moto 的 SNS 模拟无法正常工作

android - git issue Android studio-致命:不是git存储库(或任何父目录):.git

unit-testing - Gradle中的Sonarqube任务不会产生测试覆盖率

Gradle 创建一个空文件夹 `build/kotlin/sessions` ,为什么?当我删除它回来时,如何修复它?

android - 如何创建已弃用版本的 Android 虚拟设备

安卓位置: SO answers don't agree with Android Dev site