Aegis
Aegis

Reputation: 5791

sonarqube with both androidTest and test covarage

I'm trying to setup sonarqube reporting in my Android project. I currently have trouble with showing all test classes in the sonar UI, the coverage is shown in percentages and currently only the unit test from app/src/test/ are shown as Unit Tests.

My project has a test folder app/src/test/ which contains unit test and I have a androidTest folder app/src/androidTest/ which contain android unit, integration and UI tests. When I run all the tests via gradle the android-gradle plugin generates build/jacoco/testDebugUnitTest.exec and build/test-results/debug/TEST-*Test.xml which contains the jacoco results and coverage report for the unit test in the test folder. Also the android-gradle plugin generates build/outputs/code-coverage/connected/coverage.ec and build/outputs/androidTest-results/connected/TEST-*Test.xml contain the results and coverage reports from the androidTest folder

In my build.gradle I can specify the properties for the sonar plugin.

 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
        }
    }

With sonar.junit.reportsPath I can specify which xml report is sent to the sonarqube server. When I change it to build/outputs/androidTest-results/connected I get the androidTest shown as Unit Test on the dashboard. Is there a way to make the sonar plugin look in both directories or merge the results together?

Upvotes: 3

Views: 2807

Answers (1)

Thomas Keller
Thomas Keller

Reputation: 6060

Until https://jira.sonarsource.com/browse/SONAR-4101 is fixed, the only option you have is to write a task that copies your test result files into a single place and configure that as sonar.junit.reportsPath, like this:

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
                }
            }
        }
    }
}

Paths of course have to be adapted when you have flavors in your build.

Upvotes: 2

Related Questions