Search code examples
phpunitcodecov

how can I exclude files from being taked to the account by PHPUnit in coverage


I have a Codecov that listens to the test suites, let's for example say we have only an integration tests suit, the config will look something like this: codecov.yaml

coverage:
  status:
    project:
      default:
        target: 50%
        threshold: 0%
        base: auto    
        if_no_uploads: error  
        if_not_found: success
      integration:
        target: 75%
        threshold: 0%  
        base: auto    
        if_no_uploads: error 
        if_not_found: success 
        flags:
          - integration

let's say I have a lot of files called Resouser.php in different directories under /src/App/Ui/ that hold only open API documentation configs but not actual PHP code, so I want to exclude them from coverage.

If I understand correctly it needs to be done in the <coverage> section of phpunit.xml, right? So I did like this:

  <coverage cacheDirectory="var/phpunit/coverage/.phpunit.cache">
    <exclude>
      <directory suffix="Resource.php">./src/App/Ui/</directory>
    </exclude>
    <report>
      <clover outputFile="var/phpunit/coverage/clover.xml"/>
      <crap4j outputFile="var/phpunit/coverage/crap4j.xml"/>
      <html outputDirectory="var/phpunit/coverage"/>
      <xml outputDirectory="var/phpunit/coverage"/>
      <text outputFile="php://stdout" showUncoveredFiles="true"/>
    </report>

a told I wanted to exclude all the files in the directory ./src/App/Ui/ with the suffix EndpointResource.php and nothing changes, the coverage percentage is the same, and the amount of covered vs total files is still the same

Code Coverage Report:        
  2024-01-16 14:54:19        
                             
 Summary:                    
  Classes: 56.07% (134/239)  

as it was before I added exclude tags


Solution

  • If you add the following annotation on top of your classes/methods to be ignored it should work fine. Might be a bit more work than excluding a whole directory at once but it will work.

    /**
     * @codeCoverageIgnore
     */