Reputation: 2745
For a project hosted at https://www.gitlab.com I would like to setup code coverage in the CI setup, so it can be displayed in the job list
My configuration looks like this:
.gitlab-ci.yml
image: php:7.1.1
cache:
paths:
- vendor/
before_script:
# Install git, the php image doesn't have installed
- apt-get update -yqq
- apt-get install git -yqq
# Install composer
- curl -sS https://getcomposer.org/installer | php
# Install all project dependencies
- php composer.phar install
# Run our tests
test:
only:
- master
- develop
script:
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
The job succeeds, but shows the error message
Error: No code coverage driver is available
I have updated the setting for Test coverage parsing and set the regex to
^\s*Lines:\s*\d+.\d+\%
the example for PHP/PHPUnit.
When I run the command
vendor/bin/phpunit --coverage-text --colors=never
locally, I get the following output:
Code Coverage Report:
2017-06-21 14:52:55
Summary:
Classes: 100.00% (4/4)
Methods: 100.00% (14/14)
Lines: 100.00% (43/43)
\Rodacker\CartExample::Article
Methods: 100.00% ( 6/ 6) Lines: 100.00% ( 11/ 11)
\Rodacker\CartExample::ArticleLoader
Methods: 100.00% ( 2/ 2) Lines: 100.00% ( 21/ 21)
\Rodacker\CartExample::ArticleRepository
Methods: 100.00% ( 3/ 3) Lines: 100.00% ( 6/ 6)
\Rodacker\CartExample::Image
Methods: 100.00% ( 3/ 3) Lines: 100.00% ( 5/ 5)
Upvotes: 13
Views: 12613
Reputation: 589
I use Docker containers and Gitlab CI.
I have a typical PHPUnit config.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<coverage processUncoveredFiles="true">
<include>
<directory>src</directory>
</include>
<report>
<clover outputFile="phpunit.coverage.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="My Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
A part of configuration for testing. vendor/bin/phpunit --coverage-text --colors=never
and coverage: '/^\s*Lines:\s*\d+.\d+\%/'
are required for the test coverage.
testing:
image: $CI_REGISTRY/my-images/ci-docker-compose:latest
stage: test
before_script:
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
script:
- docker-compose -f docker-compose.yml up -d --build
- docker-compose exec -T my-php-container vendor/bin/phpunit --coverage-text --colors=never
coverage: '/^\s*Lines:\s*\d+.\d+\%/'
only:
- merge_requests
My Dockerfile (a part).
# some commands
RUN apk add --no-cache --virtual .build-deps autoconf file g++ gcc libc-dev pkgconf make \
&& pecl install pcov && docker-php-ext-enable pcov \
&& apk del .build-deps
# some commands
Here is important the following commands: pecl install pcov && docker-php-ext-enable pcov
. They install packages for fast test coverage.
In another way you can use xdebug
and "xdebug.mode=coverage"
or set DEBUG_MODE=coverage
in the Docker container before your tests.
Upvotes: 2
Reputation: 4598
Well, in my case I had to add a pair of parentheses (capturing group) in my coverage regex. This is an extract from my .gitlab-ci.yml
file:
phpunit:
image: php:8.0
stage: qa
before_script:
- pecl install xdebug
- docker-php-ext-enable xdebug
script: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text
coverage: /\s+Lines:\s+(\d+\.\d+%)/
Also I recommend to move all PHPUnit configuration to phpunit.xml
.
Upvotes: 2
Reputation: 11
For people who are searching why PHPUnit is not outputting coverage report when run in CI there are some changes required.
In my case it was with PHP 7.3 CLI version and phpunit 9.4.3 coverage report was missing when run vendor/bin/phpunit --configuration phpunit.xml.dist --coverage-text --colors=never
, in log file it was outputted Warning: xdebug.mode=coverage has to be set in php.ini
.
Solved it by adding xdebug.ini file with xdebug attribute xdebug.mode=coverage
to PHP conf.d folder. To find where is your php.ini file is located run php -i |grep php\.ini
Upvotes: 1
Reputation: 2745
The problem was the missing Xdebug installation in the docker image. I could not install a proper version using apt-get
, so I had to add a pecl install xdebug
call in the before_script
section:
image: php:7.1.1
cache:
paths:
- vendor/
before_script:
# Install git, the php image doesn't have installed
- apt-get update -yqq
- apt-get install git -yqq
# Install Xdebug
- pecl install xdebug
- docker-php-ext-enable xdebug
# Install composer
- curl -sS https://getcomposer.org/installer | php
# Install all project dependencies
- php composer.phar install
# Run our tests
test:
only:
- master
script:
- vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never
Upvotes: 16