Code coverage
At this stage, all our tests pass. But what does that mean regarding our application ?
Not much you may say, but can you quantify it and will you be able to tell on a real project ?
This is when test coverage becomes handy.
Integrate JaCoCo to your reporting local Website
Add the following to the file pom.xml
inside the project (after </dependencies>
)
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The report generation is now included in the build life cycle (test phase).
- Generate the report:
mvn test
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running fr.inria.sed.SphereTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.116 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.4:report (report) @ TPCISedRaJava ---
[INFO] Loading execution data file /home/user/gitlabciintroduction/java/target/jacoco.exec
[INFO] Analyzed bundle 'TPCISedRaJava' with 1 classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.459 s
[INFO] Finished at: 2019-09-04T09:32:38Z
[INFO] ------------------------------------------------------------------------
- Use your favorite Web browser (firefox, chrome, iceweasel, ...) to visualize the generated report (outside your docker container):
firefox target/site/jacoco/index.html &
Coverage Report on GitLab CI
Display coverage value
We are going to test the following command line displays the correct coverage value in your docker command line:
echo "Coverage...: $(grep -oP '(?<=</package><counter type="INSTRUCTION").*?(?=/>)' target/site/jacoco/jacoco.xml | awk -F"\"" '{print $4}') %"
Add this command to the job_unit_test
script in the .gitlab-ci.yml
file.
coverage
GitLab CIGitLab CI allows coverage. Add the following regex to the job_unit_test
section of the .gitlab-ci.yml
file:
coverage: '/Coverage\.+:\s(\d+.\d+\%)/'
Commit and push the .gitlab-ci.yml
file and check the job_unit_test
succeds.
Check on the GitLab web site, in CI / CD
, Jobs
that the coverage percentage
is appearing.
Check you can download the artifact and check in a web browser the coverage.
Unitary tests
- Add some unitary tests to improve the test coverage
- Check on the GitLab web site, in
CI / CD
,Jobs
that the coverage percentage is updated.
Home | Java Home | << Java Previous - Pre-computation | >> Java Next - Code to cover