2

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

2
  • Can you please add console log? Commented Aug 29, 2018 at 20:27
  • @TalhaJunaid there are no errors on the console. It just says the tests were completed successfully. No mentions about code coverage. I was able to come up with a solution yesterday though. Thanks!
    – dokgu
    Commented Aug 30, 2018 at 13:58

1 Answer 1

3

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.

8
  • 1
    pecl install xdebug-2.5.5 should work with PHP 5.6 Commented Aug 30, 2018 at 8:36
  • @SaschaFrinken out of curiosity, is there a way for me to only install the xdebug extension only once instead of doing it for every single time the pipeline is triggered?
    – dokgu
    Commented Aug 30, 2018 at 13:56
  • 2
    Create an extra Gitlab project where you build a php-56 docker image that extends the default php56 image and install all the extra modules (pecl etc.) you need and publish it to the gitlab registry. Now you can use this image in your ci. Commented Aug 30, 2018 at 16:20
  • I created a gitlab project (gitlab.com/sfrinken/php-56-xdebug) to show what I mean. HTH Commented Aug 30, 2018 at 22:08
  • @SaschaFrinken I get this The page could not be found or you don't have permission to view it.. Is the project public?
    – dokgu
    Commented Aug 31, 2018 at 13:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.