How to enable code coverage output in job list for PHP project on gitlab.com
Asked Answered
A

4

13

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

job list in gitlab.com project

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

no code coverage found job output

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)
Amand answered 21/6, 2017 at 12:55 Comment(2)
you are missing xdebug on your runner.Circuitous
yes, that is what I figured out as well. Had problem installing it via apt-get but it works using pecl.Amand
A
16

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
Amand answered 24/6, 2017 at 19:26 Comment(2)
Yes it does the job BUT how about the coverage report. How the Gitlab ci/cd exposes the coverage report once the test done?Mavis
@RiteshAryal - In the project's -- Settings > CI / CD > General pipelines > Test coverage parsing ... there are examples. I think that's what you're after.Mullah
M
2

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.

Motionless answered 23/11, 2021 at 11:58 Comment(0)
J
1

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

Jeroboam answered 31/12, 2020 at 11:9 Comment(0)
A
1

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.

Amalia answered 19/8, 2021 at 8:53 Comment(2)
Please, thank you. That's what I was looking for.Ilana
@Ilana glad to help you :)Amalia

© 2022 - 2024 — McMap. All rights reserved.