Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 905
lordrhodos Avatar asked Jun 21 '17 12:06

lordrhodos


1 Answers

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.

like image 108
Oleg Dmitrochenko Avatar answered Sep 22 '22 15:09

Oleg Dmitrochenko