QA Automation : API Code Coverage with JaCoCo and Sonarqube .

Note — Written this article assuming that you have an independent API automation framework which are pointing to micro-services/product application.

The code coverage plays a very important role while doing QA and product automation , isn't it? Lets see — why it is important to rely on code coverage report.

SDET and QA functional tester both are claiming that they have covered all functional features automation and it’s test cases. (let’s say they have claimed 90%) . They have decided to execute the code coverage to prove their claim and interestingly the coverage report was showing only 55%. Now, This will surely help them to find out missing test cases by analysis the report which can further help them to increase the coverage.

You will find many tools to generate code coverage report where few of them are paid and few of are open source tools. We will discuss here more about JaCoCo and how we can configure it.

JaCoCo : It is an open source code coverage analysis tool in java vm based environment . You can use either ant task or jacoco maven plugin.

SonarQube : it is an automatic code review tool to detect bugs, vulnerabilities, and code smells in your code. It can integrate with your existing workflow to enable continuous code inspection across your project branches and pull requests.

Pre-requisite :

  1. Jacoco and Sonarqube jars.

There must be two repo’s needs to be pull on your slave/vm/local machine

  • Application Under Test
  1. Jenkins and Sonarqube jars —

Download the below jars from the Jacoco platform and make sure it should get checked in your project. All these below jars should be reside under coverage folder/any folder.

Jacoco-cli-nodeps.jar

Org.jacoco.agent-0.8.5-runtime.jar

Sonar-scanner-cli-4.6.0.2311.jar

2. Jenkins :

Let’s create a Jenkins File (jenkinsfile — copy and paste below content and replace the job name accordingly). and check in to your project.

#!groovy

node(‘<<your node slave>>’){
try {
stage(“Cleanup Workspace”){ deleteDir() }
stage(“Checkout”){ checkout scm }
stage(“Build api-service”){ service() }
stage(“Run automation”){ testExecution()}
stage(“Report”){ report()}

}catch (err) {

throw err
}
}

def service(){

dir(‘api-service’) {


sh ‘cp /var/jenkins/workspace/QA_Micro-Services-Coverage/target/<<product-api>>.jar <<product-api>>.jar’
}

}

testExecution() — This method will be responsible to deploy jar on container and execute the tests and prepare coverage xml report

def testExecution() {

script {
serviceImage = docker.build ‘api-service:snapshot’
//container = serviceImage.run(‘-p 8080:8080’)
//docker.image(serviceImage).withRun { c ->
serviceImage.withRun(‘-p 8080:8080 -p 6300:6300’) { c ->
//input message: “continue?”
sh ‘netstat -tulpa’
withMaven(maven: ‘Maven_3_3_9’, jdk: ‘Java_Jenkins_JDK8u221’){
sh “mvn clean install package -Dsuite=testng_suite.xml”
}

sh ‘’’

java -jar coverage/jacoco-cli-nodeps.jar dump — destfile target/automation.exec
java -jar coverage/jacoco-cli-nodeps.jar report target/automation.exec — classfiles /var/jenkins/workspace/<<QA Job>>/target/classes — xml target/automation.xml
‘’’
}
}

}
Report () — responsible to publish a report on Sonar.

def report(){

sh ‘’’
java -Dsonar.host.url=http://<<sonar_host>>:9000 -Dsonar.projectKey=service-api -Dsonar.login=<<login>> -Dsonar.password=<<pwd>> -Dsonar.projectBaseDir=/var/jenkins/workspace/<<Jenkins job name-application>> -Dsonar.java.binaries=target -Dsonar.coverage.jacoco.xmlReportPaths=../target/automation.xml -Dsonar.java.libraries=/var/jenkins/workspace/<<Jenkins job name-automation>>/coverage/jacoco-cli-nodeps.jar -jar /var/jenkins/workspace/<Jenkins job name-automation>>/coverage/sonar-scanner-cli-4.6.0.2311.jar
‘’’
}

3. Docker File — Get the below content and create a Dcoker file and check in to your project.

FROM openjdk:9-jre

COPY api-service/<<application>>.jar /opt/app/<<application>>.jar
COPY coverage/org.jacoco.agent-0.8.5-runtime.jar /opt/app/agent.jar
WORKDIR /opt/app
RUN sh -c ‘touch /opt/app/<<application>>.jar’

EXPOSE 8080
EXPOSE 6300

ENTRYPOINT [“java”, “-javaagent:/opt/app/agent.jar=output=tcpserver,address=0.0.0.0,port=6300”,”-DAPP_HOME=/opt/app/”,”-jar”, “/opt/app/<<application>>.jar”]

4. Sonarqube Container —

  • Fire the below command to run Sonarqube on Docker
  • Generate Token — Enter the token name which will generate the token which you can use it in your pom.xml file (if you are going to use jacoco maven plugin then you can use generated token — this is out of scope for this article)

Your SonarQube setup is up and running successfully.

5. Final step to create Jenkins Jobs which is quite straightforward-

  • Create two Freestyle Jenkins job — 1. AUT and 2. Automation project

Provide your jenkins slave details under General/Restrict Where This Project Can be Run

Source Code Management :

Click on Git radio button and enter repository details , do not forget to mention your branch

Build : Enter the Maven Command

clean install -DskipTests

  • Automation Project :