Publishing Android ApplicationUnit Test Report on SonarQube (Part -2)

Publishing Android ApplicationUnit Test Report on SonarQube with Gitlab Integration(Part 2)

This is the second part of the series. If you’ve not read the first one, you can start from below.

SonarQube Integration in Android Application (Part -1)

Let us begin by understanding a few terms and what are they used for before diving in detail.

What is meant by Code Coverage?

Code coverage is a software metric used to measure how many lines of our code are executed during the unit and automated tests.

  • It helps you to measure the efficiency of test implementation
  • It offers a quantitative measurement.
  • It defines the degree to which the source code has been tested.

What is Unit Testing?

Unit Tests are typically automated tests written and run by software developers to ensure that a section of an application (known as the “unit”) meets its design and behaves as intended.

In other words, Unit Tests are lines of code written to test the actual logic written. During Unit Testing, all of the dependencies of the code are provided by some mocking frameworks such as Mockito.

Why Unit Tests are written?

  • Improve and maintain product quality with less QA’s manual effort
  • Finding software bugs early
  • Increase confidence when shipping
  • Perform routine checks
  • Easier refactoring of old code

What is Automation Testing?

Automation testing is a Software testing technique to test and compare the actual outcome with the expected outcome. This can be achieved by writing test scripts or using any automation testing tool. Test automation is used to automate repetitive tasks and other testing tasks which are difficult to perform manually.

Why Android Automation Tests are written?

  • Reduce time for executing test cases
  • Increase the productivity of your development process
  • Early bug detection, save cost on software maintenance
  • Quickly found and fix the bugs on implementation
  • Ensure the quality of software

Series Pit Stops

Prerequisites

Scope

  1. What is JaCoCo?
  2. Integrating JaCoCo in Android Application
  3. Generating Report with Code Coverage

Sonar Analyzer does not run your tests or generate reports.SonarQube uses Jacoco to import pre-generated test reports to publish on Sonar Server.

Now its time to publish the Android Application Unit Test report on Sonar Server.

1. What is JaCoCo?

JaCoCo is a free code coverage library for Java.

2. Integrating JaCoCo in Android Application

You need to update the build.gradle file to integrate JaCoCo.

a. In Project’s build.gradle file, add JaCoCo plugin at top:-

apply plugin: 'jacoco'

b. Add classpath dependencies within buildscript (buildsccript -> dependencies)

classpath "org.jacoco:org.jacoco.core:0.8.4"

c. After Step 1 and 2, hit “Sync Now”

d. Configure JaCoCo by adding the following:-

jacoco {
toolVersion = "0.8.4"
reportsDir = file("$buildDir/reports")
}

e. Add an action for task “Test”

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

f. Under build.gradle -> android -> buildTypes, add:

testCoverageEnabled true

g. Under build.gradle -> android -> testOptions, add:

unitTests.returnDefaultValues = true
unitTests.all {
jacoco {
includeNoLocationClasses = true
}
}

h. Update the SonarQube properties, by adding :

property "sonar.java.coveragePlugin", "jacoco"
property "sonar.jacoco.reportPath", "**/jacoco/*.exec"

i. Now your updated SonarQube property will look like:-

sonarqube {
properties {
property "sonar.projectName", "$PROJECT-NAME"
property "sonar.projectKey", "$PROJECT-KEY"
property "sonar.tests", ["src/test/java", "src/test/kotlin"]
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.jacoco.reportPath", "**/jacoco/*.exec"
property "sonar.test.inclusions", "**/*Test*/**"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.sources", "src/main/java"
property "sonar.exclusions", '**/*Test*/**,' +
'*.json,' +
'**/*test*/**,' +
'**/.gradle/**,' +
'**/R.class,' +
'**/R$*.class'
}
}

j. Replace PROJECT-NAME and PROJECT-KEY with the name of your Android Application

k. Hit “Sync Now”

That’s all….. you have successfully configured JaCoCo in your Android Application.

3. Generating Report with Code Coverage

As discussed, SonarQube imports the test reports pre-generated to publish on Sonar Server. You need to run your tests first before publishing reports.

a. Run the Tests written for Android Application

./gradlew test

b. Run gradle command for SonarQube

./gradlew sonarqube -Dsonar.host.url=http://localhost:9000/ -Dsonar.login=$REPLACE_WITH_GENERATED_TOKEN

The updated Sonar Dashboard will look like:-

CI Pipeline

Before going into the integration part of SonarQube CI Pipeline, first, let’s understand the CI Pipeline.

What is CI?

Continuous Integration (CI) is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early.

Why CI?

  • Code control, code versioning
  • Automated Testing, pre-deploy, post-deploy testing.
  • Defects are detected & fixed sooner
  • CI reducing the risk & Overheads
  • Prevent wrong commits, missing file & resource while committing code
  • Build automation
  • Deployment Automation

Integration of SonarQube in Android CI Pipeline

Let’s dive into the code to see how you can integrate SonarQube in Gitlab CI pipeline.

You need to update the .gitlab-ci.yml file like below:-

stages:
- test
- sonarqube

unit_tests:
stage: test
script:
- ./gradlew test
sonarqube_stage:
stage: sonarqube
script:
- ./gradlew sonarqube -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_LOGIN_KEY

Replace SONAR_HOST_URL and SONAR_LOGIN_KEY with the actual Sonar Url and Login Key.

End Note

I hope you enjoyed the post and able to set up and publish the report.

  • Don’t forget to clap your hands on both the pit stops


Publishing Android ApplicationUnit Test Report on SonarQube (Part -2) was originally published in OLX Group Engineering on Medium, where people are continuing the conversation by highlighting and responding to this story.

Source: OLX