Force Exclude files from PHPUnit Code Coverage
Asked Answered
N

6

27

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

Nuremberg answered 17/1, 2013 at 12:52 Comment(3)
Have you tried to remove * from path?Capuano
@Capuano yeah I tried that, tried just about everything, vendor, vendor/, vendor/*, vendor/*/ and vendor/*/*Nuremberg
I was just having a similar issue and the way I interpreted the above structure was to exclude the <directory>'s from being included in the whitelist. As opposed to the <blacklist><exclude> which would exclude the <directory>'s from being included in the blacklist.Housemaster
N
27

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>
Nuremberg answered 17/1, 2013 at 13:51 Comment(2)
Note to self. This needs to be inside <filter>...</filter>Arva
Blacklisting is not supported anymore by Php_Code_Coverage library.Bonedry
D
34

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>
Danelaw answered 27/8, 2018 at 9:38 Comment(1)
The suffix filter on this answer made a huge difference to the performance of my coverage report generation; thank you!Coffeecolored
N
27

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>
Nuremberg answered 17/1, 2013 at 13:51 Comment(2)
Note to self. This needs to be inside <filter>...</filter>Arva
Blacklisting is not supported anymore by Php_Code_Coverage library.Bonedry
M
2

Observation: whitelisting (the directories you want to be included in the code coverage) makes the phpunit execution of the entire test suite quicker, compared to blacklisting.

<filter>
    <whitelist>
          <directory>/my/project/directory/to/be/covered</directory>
          ....
    </whitelist>
</filter>

Tested with: PHPUnit 4.5.0 and PHP 5.5.9

Malayalam answered 20/6, 2015 at 9:4 Comment(0)
R
2

As of recent versions of PHPUnit 10.x, this seems to have changed again with the removal of the <filter> element and changes to the parent element for the <include> / <exclude> tags, requiring a new solution. According to what is in the official docs for the configuration file, the <source> element now appears to be the new basis for applying project file filters via the <include> and <exclude> tags.

Here is a mostly working phpunit.xml example in relation to the OP using the new element structure. Add the <source> block into your existing <phpunit> block contents.

<phpunit ...>
    <source>
        <include>
            <directory suffix=".php">app</directory>
            <directory suffix=".php">lib</directory>
            <!-- Add any other project directories you want included by PHPUnit here -->
        </include>
        <exclude>
            <directory suffix=".php">lib/vendor</directory>
            <!-- Filter down additional unwanted subdirectories here -->
        </exclude>
    </source>
</phpunit>
Rep answered 23/12, 2023 at 22:11 Comment(0)
V
1

These answers all seem to apply to older versions of PHPUnit

I was running PHPUnit 5.7.23 and had issues getting files included and excluded for phpunit. The syntax seems to have changed substantially and is only partially backward compatible. I had a complex requirement where i needed to also included & excluded separate directories for code coverage (its a legacy system).

Here is what I required in phpunit.xml:

<testsuites>
    <testsuite name="Unit Tests">
        <directory suffix="Test.php">./tests</directory>

        <exclude>./tests/blah/excluded_file_from_tests1.php</exclude>
        <exclude>./tests/blah/excluded_file_from_tests2.php</exclude>
        <exclude>./tests/blah/excluded_file_from_tests3.php</exclude>
    </testsuite>
</testsuites>
<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">./common/lib/included_directory</directory>
        <exclude>
            <directory suffix=".php">./common/lib/included_directory/core/blah/excluded_directory</directory>
        </exclude>
    </whitelist>
</filter>

So:

  • Under <testsuites> I'm including ./tests directory
  • But, under .tests i wanted to exclude a set of files excluded_file_from_testsX.php
  • Then, i wanted to whitelist a directory ./common/lib/included_directory
  • But, under that included directory below a few levels is a directory i also wanted to exclude (./common/lib/included_directory/core/blah/excluded_directory).

So this seemed to work well when run via phpunit --coverage-html build/coverage

Vincentvincenta answered 27/2, 2018 at 3:7 Comment(0)
T
1

Works with PHPUnit 9.0.1

<filter>
    <whitelist addUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">DIRECTORY_TO_ADD</directory>
        <directory suffix=".php">ANOTHER_DIR_TO_ADD</directory>
        <exclude>
            <file>ANOTHER_DIR_TO_ADD/file1_to_not_include.php</file>
            <file>ANOTHER_DIR_TO_ADD/file2_to_not_include.php</file>
        </exclude>
    </whitelist>
</filter>
Tyrolienne answered 12/8, 2020 at 22:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.