Force Exclude files from PHPUnit Code Coverage

前端 未结 5 2088
北恋
北恋 2021-02-19 10:31

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/ve

相关标签:
5条回答
  • 2021-02-19 10:39

    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

    0 讨论(0)
  • 2021-02-19 10:43

    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>
    
    0 讨论(0)
  • 2021-02-19 10:44

    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>
    
    0 讨论(0)
  • 2021-02-19 10:45

    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

    0 讨论(0)
  • 2021-02-19 10:48

    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>
    
    0 讨论(0)
提交回复
热议问题