Coveralls with Github and Travis

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.

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 [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; 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:

<phpunit ...>
  <!-- Add a filter to make sure we don't count venders and Tests in the coverage report -->
  <filter>
        <whitelist>
            <directory suffix=".php">./src</directory>
            <exclude>
                <directory>./vendor</directory>
                <directory>./tests</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

That is all you need to do. Happy testing!

5 Comments

  1. Very good!! This help me!!

    Reply
  2. Will this work with the recent 1.0.0 release of satooshi/php-coveralls?

    Reply
  3. I want to know, what is coverage? Like, what does Coveralls exactly tell you?

    Reply
  4. Thank you man. You help me too.

    Reply
  5. Thanks! Helpful even for v1.0.x

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>