Setup code coverage on Gitlab
Goal
How can you configure the coverture and show it on a project badge.
Java
Configure JaCoCo on the maven file
Configuration on
pom.xml
file1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>90%</minimum>
</limit>
<limit>
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>Generate reports (on your CLI, or
Run Maven Configuration
on your IDE)1
mvn clean install test jacoco:report
Configure Gitlab
- CI file on project (
gitlab-ci.yml
)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20image: maven:3.3.9-jdk-8
cache:
paths:
- /root/.m2/repository/
test:
script:
- mvn clean install
- mvn test jacoco:report
- cat target/site/jacoco/index.html |
grep -o 'Total[^%]*%' |
grep -o '<td class="ctr2">[^%]*%' |
sed -e 's/<td class="ctr2">/Jacoco-Test-Coverage:/g'
coverage: '/Jacoco-Test-Coverage:(\d+\.?\d+)%/'
artifacts:
reports:
junit:
- target/surefire-reports/TEST-*.xml
- target/failsafe-reports/TEST-*.xml
Python
Install and launch coverage
Install coverage (on your Python CLI, use
pip
)1
python -m pip install coverage
Generate reports
1
2
3
4
5
6# run
python -m coverage run -m --source="directory_to_inspect" unittest discover
# get coverage report on CLI
python -m coverage report
# get html coverage
python -m coverage html
Configure Gitlab
- CI file on project (
gitlab-ci.yml
)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20image: python:latest
before_script:
- python -V # print out python version for debugging
- python -m pip install --upgrade pip
- python -m pip install coverage
stages:
- test
test_job:
stage: test
script:
- echo "Running tests"
- python -m unittest discover -s "./tests/"
- python -m coverage run -m \
--source="directory_to_inspect" \
unittest discover
- python -m coverage report
coverage: '/^TOTAL.+?(\d+\%)$/'
NodeJS
Install and launch coverage
Install coverage (on your npm CLI, use
npm
)1
npm install nyc
Generate reports
1
2
3
4
5# you may store this a script on package.json
"scripts": {
"test": "node jasmine.js",
"coverage": "nyc --reporter=lcov --reporter=text-summary npm run test"
},
Configure Gitlab
- CI file on project (
gitlab-ci.yml
)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25image: node:12-alpine # use nodejs v12 LTS
cache:
paths:
- node_modules/
before_script:
- npm install
stages:
- test
test_job:
stage: test
script:
- npm install jasmine --save
- npm install jasmine-console-reporter --save-dev
- npm install nyc
- npm test
- npm run coverage
coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'
artifacts:
paths:
- public
only:
- master