Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Exclude files from PHPUnit Code Coverage

Is it possible to forcefully exclude a folder from PHPUnit's code coverage?

Problem I've got is, that I have a Symfony 1.4 project, which has folders at ./lib/vendor/symfony/*. I want to exclude anything that's inside ./lib/vendor/* - recursively.

Now, I want to exclude them whether they were covered implicitly by my tests or not, i.e. I never want to see these folders. So, I've added this bit to my phpunit.xml config file, but it doesn't seem to exclude these folders, no matter what I do:

<filter>
    <whitelist>
        <exclude>
            <directory>./lib/vendor/*</directory>
            <directory>./lib/vendor/symfony/lib/*</directory>
        </exclude>
    </whitelist>
</filter>

It appears to me the moment code gets hit and XDebug notices it, PHPUnit will include it in the code coverage no matter what. The downside for me with this is, that this code is already tested by Symfony developers, so no need to include it in my coverage report, messing up my numbers :P

like image 460
Sarel Avatar asked Jan 17 '13 12:01

Sarel


2 Answers

Ok, so I thought that you can have either the blacklist section OR the whitelist section, turns out you can have both, so I blacklisted those folders and it worked:

    <filter>
        <blacklist>
              <directory>./lib/vendor</directory>
              <directory>./lib/helper</directory>
        </blacklist>
    </filter>
like image 170
Sarel Avatar answered Sep 17 '22 16:09

Sarel


The correct way to exclude certain files from the coverage report in more or less recent versions of PHPUnit is to use <exclude> directive instead of <blacklist>. E.g.:

  <filter>
    <whitelist>
      <directory suffix=".php">src/</directory>
        <exclude>
           <directory suffix=".php">src/Legacy/</directory>
           <file>src/example.php</file>
        </exclude>
    </whitelist>
  </filter>

For more recent PHPUnit using newest schema this will be:

  <coverage>
    <include>
      <directory suffix=".php">src/</directory>
    </include>
    <exclude>
      <directory suffix=".php">src/Legacy/</directory>
      <file>src/example.php</file>
    </exclude>
  </coverage>
like image 23
sanmai Avatar answered Sep 19 '22 16:09

sanmai