Skip to content

Instantly share code, notes, and snippets.

@mychalvlcek
Last active June 12, 2024 09:39
Show Gist options
  • Save mychalvlcek/511e9fe379e261d5c29fc8ad21acd838 to your computer and use it in GitHub Desktop.
Save mychalvlcek/511e9fe379e261d5c29fc8ad21acd838 to your computer and use it in GitHub Desktop.
Gitlab CI test-coverage percentage results for jacoco

How to include test-coverage percentage results in the merge Request of gitlab. Example for jacoco (working for both unit & integration tests)

# .gitlab-.ci.yml

test:
  stage: test
  image: maven:3.8-openjdk-17-slim
    - mvn $MAVEN_CLI_OPTS verify
      # jacoco code-coverage reporting
    - if [ -f target/site/jacoco/index.html ]; then awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print 100*covered/instructions, "% covered" }' target/site/jacoco/jacoco.csv; fi
    - if [ -f target/site/jacoco-it/index.html ]; then awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print 100*covered/instructions, "% covered" }' target/site/jacoco-it/jacoco.csv; fi
  coverage: '/([0-9.]*) % covered/'
    - if [ -f target/site/jacoco-aggregate/index.html ]; then awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print 100*covered/instructions, "% covered" }' target/site/jacoco-aggregate/jacoco.csv; fi
  coverage: '/([0-9.]*) % covered/'
<!-- pom.xml -->
<project>
    <build>
      <plugins>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.7</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-prepare-agent-integration</id>
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report-integration</id>
                        <goals>
                            <goal>report-integration</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>merge-results</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>merge</goal>
                        </goals>
                        <configuration>
                            <fileSets>
                                <fileSet>
                                    <directory>${project.build.directory}</directory>
                                    <includes>
                                        <include>*.exec</include>
                                    </includes>
                                </fileSet>
                            </fileSets>
                            <destFile>${project.build.directory}/aggregate.exec</destFile>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-merge-report</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${project.build.directory}/aggregate.exec</dataFile>
                            <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
                        </configuration>
                    </execution>

                </executions>
            </plugin>

        </plugins>
    </build>
</project>

sources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment