‹‹ Go to homepage

Code coverage of PhpSpec on Scrutinizer

22 of December, 2014


Many people claims that code coverage is useless for specs. Rightfully so. If you are writing spec for every little feature, which you going to implement, then your code coverage, naturally, will always be 100%. Which I totally agree with, but I still find it useful to have code coverage reported in CI.

Let me explain myself. Sometimes I do get carried away and write just a little bit more code than it’s necessary for a failing spec to pass. It might be one additional condition. Or a missing case for specific attribute. Or missing error handling for wrong attribute. Or a removed spec, which had to stay. Arguably you can assume that it’s not a bug if it’s not specified (yet), but I do see value in having a reporting for this. So that I can make a conscious decision if I get decreased coverage. Just to be sure. Same as with broken build, really.

Luckily there is a plugin for that in PhpSpec. It’s also pretty easy to setup on Scrutinizer. I bet it’s as easy on Travis or Jenkins too, but I happen to use Scrutinizer, but feel free to replace last bit with configuration for CI of your choice.

Add coverage plugin into dependencies along with phpspec at composer.json:

{
  "require-dev": {
    "phpspec/phpspec": "2.1.*@dev",
    "henrikbjorn/phpspec-code-coverage": "~0.2",
  }
}

Create phpspec configuration for CI at phpspec-ci.yml:

extensions:
  - PhpSpec\Extension\CodeCoverageExtension
code_coverage:
  blacklist:
    - src/MyVendor/MyProjectDoctrineBridge
  whitelist:
    - src/MyVendor/MyProject
  format:
    - clover
  output:
    clover: coverage.clover

Edit scrutinizer config to run phpspec with a coverage report at .scrutinizer.yml:

build:
  tests:
    override:
      -
        command: 'bin/phpspec run -f progress -c phpcpec-ci.yml'
        coverage:
          file: 'coverage.clover'
          format: 'php-clover'

That’s it. Now Scrutinizer will let you know if you missed few cases and allow you to make decision early, enjoy.

comments powered by Disqus