Skip to content

Ability to run Code Coverage locally with Gradle #18509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Approximation Framework Enhancement: Update the BKD traversal logic to improve the performance on skewed data ([#18439](https://github.com/opensearch-project/OpenSearch/issues/18439))
- Support system generated ingest pipelines for bulk update operations ([#18277](https://github.com/opensearch-project/OpenSearch/pull/18277)))
- Added FS Health Check Failure metric ([#18435](https://github.com/opensearch-project/OpenSearch/pull/18435))
- Ability to run Code Coverage with Gradle and produce the jacoco reports locally ([#18509](https://github.com/opensearch-project/OpenSearch/issues/18509))

### Changed
- Create generic DocRequest to better categorize ActionRequests ([#18269](https://github.com/opensearch-project/OpenSearch/pull/18269)))
Expand Down
37 changes: 30 additions & 7 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,25 +518,48 @@ Example:

# Test coverage analysis

The code coverage report can be generated through Gradle with [JaCoCo plugin](https://docs.gradle.org/current/userguide/jacoco_plugin.html).
The code coverage report can be generated through Gradle with [JaCoCo plugin](https://docs.gradle.org/current/userguide/jacoco_plugin.html). Following are some of the ways to generate the code coverage reports locally.

For unit test:

./gradlew codeCoverageReportForUnitTest
./gradlew test
./gradlew jacocoTestReport

For unit test inside a specific module:

./gradlew :server:test
./gradlew :server:jacocoTestReport

For specific unit test inside a specific module:

./gradlew :server:test --tests "org.opensearch.search.approximate.ApproximatePointRangeQueryTests.testNycTaxiDataDistribution"
./gradlew :server:jacocoTestReport -Dtests.coverage.report.html=true

For integration test:

./gradlew codeCoverageReportForIntegrationTest
./gradlew internalClusterTest
./gradlew jacocoTestReport

For integration test inside a specific module:

./gradlew :server:internalClusterTest
./gradlew :server:jacocoTestReport

For specific integration test inside a specific module:

./gradlew :server:internalClusterTest --tests "org.opensearch.action.admin.ClientTimeoutIT.testNodesInfoTimeout"
./gradlew :server:jacocoTestReport

For the combined tests (unit and integration):
For modules with javaRestTest:

./gradlew codeCoverageReport
./gradlew :qa:die-with-dignity:javaRestTest
./gradlew :qa:die-with-dignity:jacocoTestReport -Dtests.coverage.report.html=true

To generate coverage report for the combined tests after `check` task:

./gradlew check -Dtests.coverage=true

The code coverage report will be generated in `build/codeCoverageReport`, `build/codeCoverageReportForUnitTest` or `build/codeCoverageReportForIntegrationTest` correspondingly.
The code coverage report will be generated in `$buildDir/build/reports/jacoco/test/html/`.

The report will be in XML format only by default, but you can add the following parameter for HTML and CSV format.

Expand All @@ -546,7 +569,7 @@ The report will be in XML format only by default, but you can add the following

For example, to generate code coverage report in HTML format and not in XML format:

./gradlew codeCoverageReport -Dtests.coverage.report.html=true -Dtests.coverage.report.xml=false
./gradlew internalClusterTest -Dtests.coverage.report.html=true -Dtests.coverage.report.xml=false

Apart from using Gradle, it is also possible to gain insight in code coverage using IntelliJ’s built-in coverage analysis tool that can measure coverage upon executing specific tests. Eclipse may also be able to do the same using the EclEmma plugin.

Expand Down
39 changes: 38 additions & 1 deletion gradle/code-coverage.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,54 @@ tasks.withType(JacocoReport).configureEach {
}
}

// Enhance jacocoTestReport task to include all test types
allprojects {
plugins.withId('jacoco') {
tasks.matching { it.name == 'jacocoTestReport' }.configureEach {
def executionDataFiles = []
def sourceSetsList = []
if (tasks.findByName('test')) {
executionDataFiles.add("$buildDir/jacoco/test.exec")
sourceSetsList.add(sourceSets.test)
}
if (tasks.findByName('internalClusterTest')) {
executionDataFiles.add("$buildDir/jacoco/internalClusterTest.exec")
sourceSetsList.add(sourceSets.internalClusterTest)
}
if (tasks.findByName('javaRestTest')) {
executionDataFiles.add("$buildDir/jacoco/javaRestTest.exec")
sourceSetsList.add(sourceSets.javaRestTest)
}
if (!executionDataFiles.isEmpty()) {
executionData.setFrom(files(executionDataFiles).filter { it.exists() })
sourceSets(*sourceSetsList)
}
onlyIf {
file("$buildDir/jacoco/test.exec").exists() ||
file("$buildDir/jacoco/internalClusterTest.exec").exists() ||
file("$buildDir/jacoco/javaRestTest.exec").exists()
}
}
}
}

if (System.getProperty("tests.coverage")) {
reporting {
reports {
testCodeCoverageReport(JacocoCoverageReport) {
testSuiteName = "test"
}
testCodeCoverageReportJavaRestTest(JacocoCoverageReport) {
testSuiteName = "javaRestTest"
}
}
}

// Attach code coverage report task to Gradle check task
project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME).configure {
dependsOn tasks.named('testCodeCoverageReport', JacocoReport)
dependsOn(
tasks.named('testCodeCoverageReport', JacocoReport),
tasks.named('testCodeCoverageReportJavaRestTest', JacocoReport)
)
}
}
Loading