PHP coverage
Use PHPUnit to emit Cobertura XML and upload the report after the test suite completes.
Install dependencies
composer require --dev phpunit/phpunit
Generate coverage
vendor/bin/phpunit --coverage-cobertura coverage/cobertura.xml
GitHub Actions workflow
Add COVERAGE_UPLOAD_TOKEN as a GitHub secret. PHP coverage requires Xdebug or PCOV in the runner. The action uses the default upload endpoint unless you provide an optional endpoint override.
name: coverage
on:
pull_request:
push:
jobs:
coverage:
if: github.event_name == 'pull_request' || github.ref_name == github.event.repository.default_branch
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: "8.3"
coverage: xdebug
- run: composer install --no-interaction --prefer-dist
- run: mkdir -p coverage
- run: vendor/bin/phpunit --coverage-cobertura coverage/cobertura.xml
- name: Upload coverage
uses: 9to5/9to5-coverage-action@v1
with:
token: ${{ secrets.COVERAGE_UPLOAD_TOKEN }}
path: coverage/cobertura.xml
Pull request behavior
Pull requests receive checks for project coverage and changed-line patch coverage after the Cobertura report is uploaded.
Troubleshooting
If PHPUnit cannot write coverage, confirm Xdebug or PCOV is enabled and that your phpunit.xml includes the source directories.