Code coverage is a measurement of how many lines of code are executed under the test suites.
It is recommended to use JaCoCo library to generate the code coverage report. The following guide may help you use JaCoCo in OpenSearch plugins with ease.
- Add JaCoCo Gradle plugin to
build.gradle
in your repository. example - Set Jacoco to export coverage report in
xml
format. example - Call gradle task "jacocoTestReport" to generate report (
./gradlew jacocoTestReport
) after running your tests. (Or add adependsOn
to make "jacocoTestReport" task depending on your test tasks. example)
Since OpenSearch uses Gradle as the build tool, we don't recommend using Maven. Code coverage for a Maven project can be generated by JaCoCo Maven plugin, and here is an example.
- JaCoCo Gradle plugin can not collect code coverage properly for integration tests (tests that extend
OpenSearchIntegTestCase
orOpenSearchRestTestCase
, see here to learn more). However, there is a workaround to collect coverage when the integration test is running on a local single node cluster. For your reference, this file is a custom Gradle plugin to help achieve, and it can be applied conditionally like here. - Additional work is needed to get coverage report by JaCoCo for the test classes which use PowerMock, because those classes must be instrumented before the test executes. Please see the PowerMock wiki for detail, and there is an example for using JaCoCo Maven plugin. But for JaCoCo Gradle plugin, there is no native support, see the issue in
gradle
repository for detail. - JaCoCo Gradle plugin can not merge code coverage reports from different sub-projects out-of-box, when the code repository contains a Gradle multi-project build. If you need the aggrectiated code coverage report from multiple sub-projects, please check the example of a custom Gradle plugin in OpenSearch Alerting plugin, or the sample from Gradle.
OpenSearch Dashboards mainly uses Jest for unit testing, and plugins are recommended to utilize Jest as the unit testing framework.
To generate code coverage report under Jest framework, adding the flag --coverage
in the command to run unit tests. See here for an example in the CI workflow.
OpenSearch/Dashboards plugins are using Codecov for code coverage reporting and analysis. The dashboard can be seen here.
All the OpenSearch plugins are required to have the following items:
- Generate code coverage report in
xml
format in a CI workflow of Github Actions - Upload the report to Codecov by adding a step of codecov-action in the workflow.
- For private repositories, an upload token is needed, visit the repository page of Codecov to get your token: https://codecov.io/gh/opensearch-project/REPO-NAME-HERE. Please follow Github docs to use Github Secrets to store the token and not expose to the public.
- Set the workflow to be triggered by
push
andpull request
onmain
and release branches. - To see your code coverage results from Codecov, visiting https://codecov.io/gh/opensearch-project/REPO-NAME-HERE after the CI workflow is triggered.
See CI Workflows for example.
See here for an example of the status badge.
Add the following line at the header of the README markdown file of the repository:
[![codecov](https://codecov.io/gh/opensearch-project/REPO-NAME-HERE/branch/main/graph/badge.svg)](https://codecov.io/gh/opensearch-project/REPO-NAME-HERE)
Link of the badge can also be accessed from the repository's settings page of Codecov: https://app.codecov.io/gh/opensearch-project/REPO-NAME-HERE/settings/badge
- Add a
codecov.yml
file into the repository (example) - Set the target code coverage properly in case it blocks your PR in Github checks.
See Codecov docs to learn more about codecov yaml file and common configurations.