How to Use JaCoCo Report with GitHub Actions

Oct 22, 2021 | Programming

In the realm of software development, ensuring code quality is paramount. JaCoCo Report offers an efficient way to track code coverage in your projects, providing insights directly in your pull requests. This article will guide you on how to implement and customize the JaCoCo report using GitHub Actions.

What is JaCoCo Report?

JaCoCo (Java Code Coverage) is a library that helps in measuring the code coverage of Java programs. By integrating JaCoCo with GitHub Actions, you can automate the process of generating and publishing coverage reports as comments in your pull requests. This allows developers to see the impact of their contributions at a glance.

Usage

Pre-requisites

You will need to create a workflow .yml file in your repository’s .github/workflows directory. Here’s a quick example to get you started:


name: Measure Coverage
on:
  pull_request:
jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8
      - name: Run Coverage
        run: 
          chmod +x gradlew
          ./gradlew testCoverage
      - name: Add coverage to PR
        id: jacoco
        uses: madrappsjacoco-report@v1.7.1
        with:
          paths:
            ${{ github.workspace }}/build/reports/jacoco/prodNormalDebugCoverage.xml,
            ${{ github.workspace }}/build/reports/jacoco/debugCoverage.xml
          token: ${{ secrets.GITHUB_TOKEN }}
          min-coverage-overall: 40
          min-coverage-changed-files: 60

Understanding the Coverage Report

The JaCoCo report summarizes how much of your code is tested through unit tests. Think of your code as a garden; the coverage indicates how much of the garden is trimmed and looking good. If the coverage is low, it’s like seeing weeds popping up – indicating areas that need more attention and care.

Customizing Inputs

There are several customizable inputs you can utilize:

  • **paths**: Comma-separated paths of the generated JaCoCo XML files.
  • **token**: Your GitHub personal token for making comments on pull requests.
  • **min-coverage-overall**: Default is 80% for the overall project coverage requirement.
  • **min-coverage-changed-files**: Default is 80%, focusing on files that have been changed.
  • **update-comment**: Set to true to update existing comments on the pull request.
  • **title**: Customize the title of the coverage report comment.
  • **pass-emoji**: Emoji indicating a pass status when coverage meets the minimum.
  • **fail-emoji**: Emoji indicating a fail status when coverage does not meet the minimum.

Example Cases

1. Failing Workflow on Low Coverage

If you want your workflow to fail if the minimum coverage is not met, you can add an additional step that utilizes the output from the JaCoCo action:


- name: Fail PR if overall coverage is less than 80%
  if: ${{ steps.jacoco.outputs.coverage-overall < 80.0 }}
  uses: actions/github-script@v6
  with:
    script: |
      core.setFailed("Overall coverage is less than 80%!")

2. Updating Comments in PR

To avoid cluttering comments with each commit, set the update-comment input to true along with the title. This keeps your pull request comments clean and organized.

Troubleshooting

  • If the PR is created by bots like dependabot, ensure the GITHUB_TOKEN has sufficient access to write the coverage comment. Refer to the GitHub issue for further details.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

Integrating JaCoCo with GitHub Actions is a powerful way to enhance your code's quality assurance. By setting up automated coverage reports, you create a feedback loop that encourages best practices among developers.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox