Search code examples
unit-testinggitlabcode-coveragegitlab-cigitlab-ci-runner

Code coverage is always unknown in GitLab


I'm trying to understand the GitLab Pipelines and after a few tries I was able to successfully automate my unit tests. Now I'm trying to add the code coverage badge into my project and/or readme file but it always seems to show unknown.

Files:

+ application
+ system
- unit-tests
  - tests
      UtilTest.php
    autoload.php
    phpunit
  .gitignore
  .gitlab-ci.yml
  .htaccess
  index.php
  readme.md

.gitlab-ci.yml:

image: php:5.6

stages:
  - test

app:unit-tests:
  stage: test
  script:
    - php ./unit-tests/phpunit --bootstrap ./unit-tests/autoload.php ./unit-tests/tests
  coverage: '/Code Coverage: \d+\.\d+/'

On the project's Test coverage parsing section I have this set up:

enter image description here


Solution

  • So I was able to fix this by using PHP 7.2 as the Docker image and installing xdebug on the before_script call.

    .gitlab-ci.yml:

    image: php:7.2
    
    stages:
      - test
    
    before_script:
      - pecl install xdebug
      - docker-php-ext-enable xdebug
    
    app:unit-tests:
      stage: test
      script:
      - php ./unit-tests/phpunit --bootstrap ./unit-tests/autoload.php ./unit-tests/tests --coverage-text --colors=never
      coverage: '/^\s*Lines:\s*\d+.\d+\%/'
    

    I had to use PHP 7.2 because when I tried running pecl install xdebug it said it requires PHP 7. Ideally I would like to use PHP 5.6 because that's what our current server has just so the tests are on similar versions but I'll leave it as it is for now.

    I had to add --coverage-text --colors=never on the script call for it to output the numbers. Then on the coverage call I changed it to '/^\s*Lines:\s*\d+.\d+\%/' which I also used under the Test coverage parsing section on the project settings.

    And now the code coverage properly shows me my expected values.