Generate a code coverage report in HTML for a Java project using Gradle and JaCoCo.
Step 1: Project Structure
Lets assume you have a directory structure like this:
my-java-project/
├── build.gradle
└── src/
└── main/
└── test/
Step 2: Add Dependencies
Edit your build.gradle
file to include the JaCoCo plugin and configure it. Here's a sample build.gradle
file:
plugins {
id 'java'
id 'jacoco'
}
repositories {
jcenter()
}
dependencies {
// Add your project dependencies here
}
test {
useJUnitPlatform()
}
jacocoTestReport {
reports {
xml.enabled true
html.enabled true
}
}
Step 3: Write Tests
Write your Java unit tests in the src/test/
directory.
Step 4: Generate Code Coverage Report
Run the following command in the terminal within your project directory to execute tests and generate code coverage reports:
./gradlew clean test jacocoTestReport
This command will clean the project, run tests, and generate JaCoCo code coverage reports.
Step 5: Access the HTML Report
After running the command, you can find the HTML code coverage report in the following directory:
build/reports/jacoco/test/html/index.html
Open the index.html
file in a web browser to view the code coverage report. It will display detailed information about which parts of your code were covered by your tests and which were not.
Commit the the entire code coverage folder in your repo.