Skip to content

Commit 1903747

Browse files
Project Code Coverage
Signed-off-by: Prudhvi Godithi <[email protected]>
1 parent 0538b6b commit 1903747

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5252
- Approximation Framework Enhancement: Update the BKD traversal logic to improve the performance on skewed data ([#18439](https://github.com/opensearch-project/OpenSearch/issues/18439))
5353
- Support system generated ingest pipelines for bulk update operations ([#18277](https://github.com/opensearch-project/OpenSearch/pull/18277)))
5454
- Added FS Health Check Failure metric ([#18435](https://github.com/opensearch-project/OpenSearch/pull/18435))
55+
- Ability to run Code Coverage with Gradle and produce the jacoco reports locally ([#18509](https://github.com/opensearch-project/OpenSearch/issues/18509))
5556

5657
### Changed
5758
- Create generic DocRequest to better categorize ActionRequests ([#18269](https://github.com/opensearch-project/OpenSearch/pull/18269)))

TESTING.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,22 +518,48 @@ Example:
518518

519519
# Test coverage analysis
520520

521-
The code coverage report can be generated through Gradle with [JaCoCo plugin](https://docs.gradle.org/current/userguide/jacoco_plugin.html).
521+
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.
522522

523523
For unit test:
524524

525+
./gradlew test
525526
./gradlew jacocoTestReport
526527

528+
For unit test inside a specific module:
529+
530+
./gradlew :server:test
531+
./gradlew :server:jacocoTestReport
532+
533+
For specific unit test inside a specific module:
534+
535+
./gradlew :server:test --tests "org.opensearch.search.approximate.ApproximatePointRangeQueryTests.testNycTaxiDataDistribution"
536+
./gradlew :server:jacocoTestReport -Dtests.coverage.report.html=true
537+
527538
For integration test:
528539

540+
./gradlew internalClusterTest
541+
./gradlew jacocoTestReport
542+
543+
For integration test inside a specific module:
544+
545+
./gradlew :server:internalClusterTest
546+
./gradlew :server:jacocoTestReport
547+
548+
For specific integration test inside a specific module:
549+
550+
./gradlew :server:internalClusterTest --tests "org.opensearch.action.admin.ClientTimeoutIT.testNodesInfoTimeout"
529551
./gradlew :server:jacocoTestReport
530552

553+
For modules with javaRestTest:
554+
555+
./gradlew :qa:die-with-dignity:javaRestTest
556+
./gradlew :qa:die-with-dignity:jacocoTestReport -Dtests.coverage.report.html=true
531557

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

534560
./gradlew check -Dtests.coverage=true
535561

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

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

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

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

546-
./gradlew jacocoTestReport -Dtests.coverage.report.html=true -Dtests.coverage.report.xml=false
572+
./gradlew internalClusterTest -Dtests.coverage.report.html=true -Dtests.coverage.report.xml=false
547573

548574
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.
549575

gradle/code-coverage.gradle

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,28 @@ tasks.withType(JacocoReport).configureEach {
3434
}
3535
}
3636

37-
// Enhance jacoco report tasks to include all test types
37+
// Enhance jacocoTestReport task to include all test types
3838
allprojects {
3939
plugins.withId('jacoco') {
40-
// Configure both jacocoTestReport and testCodeCoverageReport tasks
41-
tasks.matching { it.name == 'jacocoTestReport' || it.name == 'testCodeCoverageReport' }.configureEach {
42-
// Collect execution data files from all available test tasks
40+
tasks.matching { it.name == 'jacocoTestReport' }.configureEach {
4341
def executionDataFiles = []
4442
def sourceSetsList = []
45-
4643
if (tasks.findByName('test')) {
4744
executionDataFiles.add("$buildDir/jacoco/test.exec")
4845
sourceSetsList.add(sourceSets.test)
4946
}
50-
5147
if (tasks.findByName('internalClusterTest')) {
5248
executionDataFiles.add("$buildDir/jacoco/internalClusterTest.exec")
5349
sourceSetsList.add(sourceSets.internalClusterTest)
5450
}
55-
5651
if (tasks.findByName('javaRestTest')) {
5752
executionDataFiles.add("$buildDir/jacoco/javaRestTest.exec")
5853
sourceSetsList.add(sourceSets.javaRestTest)
5954
}
60-
61-
// Set execution data and source sets
6255
if (!executionDataFiles.isEmpty()) {
6356
executionData.setFrom(files(executionDataFiles).filter { it.exists() })
6457
sourceSets(*sourceSetsList)
6558
}
66-
67-
// Only run if at least one execution data file exists
6859
onlyIf {
6960
file("$buildDir/jacoco/test.exec").exists() ||
7061
file("$buildDir/jacoco/internalClusterTest.exec").exists() ||
@@ -74,7 +65,6 @@ allprojects {
7465
}
7566
}
7667

77-
7868
if (System.getProperty("tests.coverage")) {
7969
reporting {
8070
reports {
@@ -88,4 +78,13 @@ if (System.getProperty("tests.coverage")) {
8878
project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME).configure {
8979
dependsOn tasks.named('testCodeCoverageReport', JacocoReport)
9080
}
81+
82+
// To include javaRestTest in code coverage
83+
allprojects {
84+
plugins.withId('opensearch.java-rest-test') {
85+
rootProject.tasks.named('testCodeCoverageReport').configure {
86+
dependsOn tasks.named('javaRestTest')
87+
}
88+
}
89+
}
9190
}

0 commit comments

Comments
 (0)