0

Azure DevOps Pipelines only supports JaCoCo and Cobertura coverage report formats :

PHPUnit only supports Clover, Crap4jn PHP, (custom) XML, HTML and TXT coverage report formats :

How can I publish the coverage result of my PHPUnit tests in my Pipeline ?

2 Answers 2

1

PHPUnit 9.4 added support for Cobertura coverage output. However the default Ubuntu build agents that Azure Pipelines provides atm. only support phpunit 8.5. But you can get coverage reports by running phpunit 9.4+ inside a docker container instead. Here is a snippet of my current azure build pipeline that does that:

trigger:
- master

pool:
  vmImage: ubuntu-latest

variables:
  phpVersion: 7.4
  phpunitImage: jitesoft/phpunit:7.4-9

steps:
- script: |
    sudo update-alternatives --set php /usr/bin/php$(phpVersion)
    sudo update-alternatives --set phar /usr/bin/phar$(phpVersion)
    sudo update-alternatives --set phpdbg /usr/bin/phpdbg$(phpVersion)
    sudo update-alternatives --set php-cgi /usr/bin/php-cgi$(phpVersion)
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar$(phpVersion)
    php -version
  displayName: 'Use PHP version $(phpVersion)'

# Do a composer install to get an autoloader that phpunit can use
- script: composer install --no-interaction --prefer-dist
  displayName: 'composer install'

# Run the test using the jitesoft phpunit docker image to get support
# for phpunit 9+ and that way cobertura reports for code coverage.
- script: |
    docker run --rm -v $(pwd):/app ${{ variables.phpunitImage }} phpunit --log-junit .junit/TEST-phpunit-junit.xml --coverage-cobertura=.coverage/COVERAGE-phpunit-cobertura.xml
  displayName: 'Run tests with phpunit docker container'

- task: PublishTestResults@2
  displayName: 'Publish test report'
  condition: always()
  inputs:
    testResultsFormat: 'JUnit'
    testResultsFiles: '**/TEST-phpunit-*.xml'
    searchFolder: '$(System.DefaultWorkingDirectory)/.junit'
    failTaskOnFailedTests: true

- task: PublishCodeCoverageResults@1
  displayName: 'Publish coverage report'
  condition: always()
  inputs:
    codeCoverageTool: 'Cobertura'
    summaryFileLocation: '$(System.DefaultWorkingDirectory)/.coverage/COVERAGE-phpunit-*.xml'
    pathToSources: '$(System.DefaultWorkingDirectory)/src'
    failIfCoverageEmpty: true

note the always() condition in the Publish* tasks. This is needed because if a test fails, then the docker run step will fail with a bash exit code 1, which in turn would prevent the report publishing unless those steps are forced. There might be a way to handle the exit code cleaner, but I haven't figured it out yet.

ps. ideally you'd do some caching as well so the docker image is not always downloaded, but I skipped that part to keep the example focused on the actual running of unit tests and coverage reports.

Sign up to request clarification or add additional context in comments.

Comments

0

As of this time, however, publishing PHPUnit code coverage result in Pipeline is not supported.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.