What is Code Coverage?
Often referred to as, but not to be confused with Test Coverage, Code Coverage refers to the percentage of your code that is executed by automated tests (unit, feature, ...). It is a metric that helps you understand how much of your code is being tested and can be used to identify areas of your codebase that are in need of tests, to ensure functionality and quality.
Code Coverage is generally calculated on a per-line basis, meaning that if a line of code is executed by a test, it is considered covered. Often when working with pull requests, you also want to track Patch Coverage, which refers to the coverage of changed lines in a commit or pull request, rather than the entire codebase.
Tracking Code Coverage
OtterWise helps you track Code Coverage by integrating with your test framework and collecting the output of your test runs. Here we will show examples in a few languages to help you get started.
Requirements
First you must add the OtterWise token for your repository, found under the Setup instructions or in your repository settings, as an environment variable in your CI workflow.
For GitHub actions this will be inside Repository settings → Secrets & Variables → Actions → New repository secret.
PHP
PHP has a few popular test frameworks that support Code Coverage, such as PHPUnit and Pest. Below is an example of how you can generate a Code Coverage report with PHPUnit and submit it to OtterWise for processing. You can see an example repository here.
We will assume you have a working CI workflow that executes your PHPUnit test suite. This can for example be a GitHub Actions or ChipperCI workflow. The PHPUnit part of your workflow might look like this:
- name: Run Tests run: vendor/bin/phpunit
To make this generate and submit a code coverage you need to do three things:
-
Enabling XDebug or Pcov
You need a coverage collector, two supported ones are Xdebug and Pcov.
For GitHub actions, this can be done in your setup php command. It might look something like this:
- name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: 8.3 coverage: pcov
-
Modifying your PHPUnit command
Now that you have added the Pcov extension, you can modify the PHPUnit command to generate a coverage report:
- name: Run Tests run: vendor/bin/phpunit --coverage-clover=build/logs/clover.xml
-
Add OtterWise upload script
We can now upload the generated coverage report to OtterWise by adding a new step to our CI workflow, after the PHPUnit test run:
- name: Upload Coverage env: OTTERWISE_TOKEN: ${{ secrets.OTTERWISE_TOKEN }} run: bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh)