Coveralls with Github and Travis

2 minute read

The other day when I was making a PR to a Github project I got a comment like this:

Coverage decreased (-0.87%) when pulling f285fe0 on Nyholm:patch-1 into 5506244 on some-project:master. </blockquote>

This was an automated message from Coveralls.io. It is a tool made for Ruby to keep track of your test code coverage. I think it is a nice way of telling the pull request author: "You need to add tests".

If you want to install Coveralls to your PHP project there is a few step you need to do. But first of, make sure you use Github and Travis.

Step 1 - Register your project at Coveralls

Go to Coveralls.io and login with your Github account. You will get a list of your repositories (just like in Travis) and then select those you want to use with Coveralls.

Step 2 - Tell Travis to tell Coveralls

You need to tell Travis to send data to Coveralls. This is done with the satooshi/php-coveralls library. This is a example .travis.yml file
language: php


php:
  - 5.3
  - 5.4
  - 5.5
  - 5.6
  - hhvm


install:
  - composer require satooshi/php-coveralls:~0.6@stable


before_script:
  - mkdir -p build/logs


script:
  - phpunit --coverage-clover build/logs/clover.xml


after_success:
  - sh -c 'if [ &quot;$TRAVIS_PHP_VERSION&quot; != &quot;hhvm&quot; ]; then php vendor/bin/coveralls -v; fi;'
I start by installing satooshi/php-coveralls. Then I make sure I got a folder to store the reports. I run PHPUnit and make sure it is generating some the reports. At last we run coveralls (but not on HHVM). Easy as pie =)

Step 3 - Optional

You may want to update you phpunit.xml.dist. Do this to make sure the proper files and directories are used when calculating the code coverage.  See this example:
&lt;phpunit ...&gt;
  &lt;!-- Add a filter to make sure we don't count venders and Tests in the coverage report --&gt;
  &lt;filter&gt;
        &lt;whitelist&gt;
            &lt;directory suffix=&quot;.php&quot;&gt;./src&lt;/directory&gt;
            &lt;exclude&gt;
                &lt;directory&gt;./vendor&lt;/directory&gt;
                &lt;directory&gt;./tests&lt;/directory&gt;
            &lt;/exclude&gt;
        &lt;/whitelist&gt;
    &lt;/filter&gt;
&lt;/phpunit&gt;
That is all you need to do. Happy testing!

Leave a Comment