Bogdan  Dubyk
Bogdan Dubyk

Reputation: 5564

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

Upvotes: 1

Views: 183

Answers (2)

Sanjay
Sanjay

Reputation: 391

in your yaml file introduce :

Ignoring Specific File Types This rule will ignore any file at any depth that ends in ".py".

ignore:
    - "**/*.py"

Ignoring Specific File Names:

ignore:
    - "**/coverage*"

Ignoring Specific Files At All Depths:

ignore:
    - "**/test_*.rb"

For reference:
Ignoring a Specific Folder

ignore:
    - "project2"

sample:

# sample regex patterns
ignore:
  - "path/to/folder"  # ignore folders and all its contents
  - "test_.*.rb"       # regex accepted
  - "**/*.py"         # glob accepted

https://docs.codecov.com/docs/ignoring-paths

Upvotes: 0

Dirk Scholten
Dirk Scholten

Reputation: 1048

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

Upvotes: 1

Related Questions