Check coverage with Codecov in Java + Gradle + Wercker configuration

It has a strong aspect as my work record, but I have created a work procedure for the content of the title, so I will post it.

Overview

This is the procedure for committing coverage measurement results to Codecov through the Wercker build phase in the GitHub repository of Java applications using Gradle.

Codecov

URL

https://codecov.io/gh

Reason for selection

I was wondering which one to use with coveralls, but I agreed with Reference article and decided to use Codecov.

Jacoco

Used for coverage measurement. Run from Gradle.

About version 0.8

Various options have been added to the new Java syntax, such as whether to cover it. Reference article

About unspecified constructor coverage

For implicit constructors for which no constructor is declared, if the test code for the constructor (instantiation) is not written, Jacoco will treat it as non-passing and the coverage rate will decrease. For example, it can happen in a utility class with static methods. For classes that are not instantiated, the constructor should be carefully private, although it may be a bit redundant. Then Jacoco will treat the constructor as out of coverage and the coverage coverage will not drop unnecessarily. If you declare the constructor explicitly, it should be used from the product code, so be sure to write the test code properly.

It seems that it was supported from Jacoco 0.8.0

table of contents

  1. Add GitHub repository to Codecov
  2. Added description for jacoco test to build.gradle
  3. Added description to send coverage to Codecov in wercker.yml
  4. Create codecov.yml and describe the OK conditions for coverage measurement results.
  5. Commit your additions and get the Wercker build running
  6. Confirm the coverage transmission to Codecov
  7. Force Codecov check on GitHub repository and reject merge if coverage measurement result is NG
  8. Get the Codecov badge

Added GitHub repository to Codecov

  1. Go to https://codecov.io/gh
  2. You can log in with your GitHub, BitBucket, or GitLab account ――This article is GitHub, so select GitHub
  3. The repositories available for the selected account will be displayed, so select the target repository.
  4. The token for sending coverage information to Codecov is displayed.
  5. Keep the screen open as you will use the token later.

Added description for Jacoco test to build.gradle

  1. Add the following to build.gradle.

    //Specify jacoco plugin for codecov
    apply plugin: 'jacoco'
    
    jacoco {
        //Version information is http://www.eclemma.org/jacoco/See
        toolVersion = "0.8.1"
    }
    
    jacocoTestReport {
        reports {
            xml.enabled = true
            html.enabled = true
        }
    }
    
    //When you run gradle build, you also run coverage measurement with Jacoco
    build.dependsOn jacocoTestReport
    
  2. In the reference sample, the Java version of targetCompatibility was also described, so I added the following as well.

    targetCompatibility = 10
    

entire build.gradle

Looking back, including the accumulation so far.

//Specify java plugin to build main and test respectively
apply plugin: 'java'

//Specify a war plugin for packaging to war
apply plugin: 'war'

//Specify the application plug-in to specify the executable class and the class that has the executable main method.
apply plugin: 'application'
//Specify the execution target class
mainClassName = 'kentfordevgithub.helloworld.HelloWorld'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 10
targetCompatibility = 10

//Specify the repository to use
repositories {
    //Use Maven repository
    mavenCentral()
}

dependencies {
    // JUnit
    // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0'
    // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.2.0'
    // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.2.0'
}

//Test run-time settings in Gradle
test {
    //Settings for running Junit5 with Gradle, Gradle4.Can be described because Gradle natively supports Junit 5 from 6
    useJUnitPlatform()
    //Number of parallel test threads(Number of concurrent executions)The set(1 thread for each test class)
    maxParallelForks = 4
}

//Specify IDE plug-ins to support each IDE
apply plugin: 'eclipse'
apply plugin: 'idea'

//Specify jacoco plugin for codecov
apply plugin: 'jacoco'

jacoco {
    //Version information is http://www.eclemma.org/jacoco/See
    toolVersion = "0.8.1"
}

jacocoTestReport {
    reports {
        xml.enabled = true
        html.enabled = true
    }
}

//When you run gradle build, you also run coverage measurement with Jacoco
build.dependsOn jacocoTestReport

Added description to send coverage to Codecov in wercker.yml

  1. Add the following to the build block. --Added command to send coverage measurement results to Codecov after building with Gradle -$ CODECOV_TOKEN is an environment variable to refer to the token displayed above in Codecov --The environment variable name may be changed if any other environment variable name is acceptable.

    - script:
        name: post coverage to codecov
        code: |
          bash <(curl -s https://codecov.io/bash) -t $CODECOV_TOKEN
    
  2. Add Codecov token to Wercker build phase environment variables

  3. Open the target application screen of Wercker

  4. Open the Workflows tab

  5. Open Pipelines build --To use environment variables only in build --Do not set in the overall flow to minimize the range of environment variables used

  6. Open the <> Environment tab

  7. Enter the above environment variable name CODECOV_TOKEN in Key. --If you use any other environment variable name, use that name.

  8. Enter the token in Value

  9. Check Protected --The value cannot be confirmed from the Wercker browser page. --If this environment variable is output to the display during step execution by wercker.yml, a step execution error will occur.

  10. Click the Add button to complete the addition

entire wercker.yml

Looking back, including the accumulation so far.

# This references an OpenJDK container from the
# Docker Hub https://hub.docker.com/_/openjdk/
# Read more about containers on our dev center
# http://devcenter.wercker.com/docs/containers/index.html
box: openjdk:10.0.1-jdk

# defining the dev pipeline
dev:
  steps:
    # A step that executes `gradle bootRun` command
    - script:
      name: run gradle
      code: |
        ./gradlew bootRun

# Build definition
build:
  # The steps that will be executed on build
  steps:
    # A step that executes `gradle build` command
    - script:
        name: run gradle
        code: |
          ./gradlew --full-stacktrace -q --project-cache-dir=$WERCKER_CACHE_DIR build
    - script:
        name: post coverage to codecov
        code: |
          bash <(curl -s https://codecov.io/bash) -t $CODECOV_TOKEN

Create codecov.yml and describe the OK conditions for coverage measurement results

  1. Create codecov.yml in the root directory of the repository

  2. Describe the following. --For the threshold value, refer to the one set in RxJava. --If you have any aim, set it by referring to Specifications. --If the coverage measurement result is lower than the target and threshold values, it is treated as NG.

    codecov:
      notify:
        require_ci_to_pass: yes
    
    coverage:
      status:
        project:
          default:
            target: 95%
            threshold: 1%
    
        patch:
          default:
            target: 95%
            threshold: 1%
    
        changes: no
    

Commit your additions and get the Wercker build running

  1. Commit and push the addition
  2. Since the automatic build and automatic test to Wercker should be linked in the previous article, the coverage measurement result transmission to Codecov also works.

Confirm coverage transmission to Codecov

  1. On the screen of the target repository of Codecov, the coverage measurement result when committing above is added.
  2. If you open the details, you can see the result in a graph etc.

Force Codecov check on GitHub repository and reject merge if coverage measurement result is NG

  1. Open Settings in the GitHub repository
  2. Open Branches
  3. Select and open the master branch
  4. Check the following in Require status checks to pass before merging
    • codecov/patch
    • codecov/project
  5. Subsequent pull requests to master cannot be merged unless the Codecov result is OK

When the check result is sent slowly from Codecov

Wercker's Worflow execution result is reflected immediately, but it takes some time for the check result from Codecov to be reflected. In that case, you can manually run the notification from the Build tab on the Codecov commit details screen.

Get the Codecov badge

  1. You can get a link to display the badge from the Setting screen
  2. Display the coverage measurement result at the top of the repository by describing it in README.md.

Recommended Posts

Check coverage with Codecov in Java + Gradle + Wercker configuration
Static code analysis with Checkstyle in Java + Gradle
Build and test Java + Gradle applications with Wercker
Build Java with Wercker
Java multi-project creation with Gradle
Check https connection in Java
[Java 8] Duplicate deletion (& duplicate check) with Stream
Build a Java project with Gradle
Morphological analysis in Java with Kuromoji
Do HelloWorld in Java / IntelliJ / Gradle
Output test coverage with clover + gradle
[Java] Element existence check with Stream
Check Java parameters in Kubernetes pods
Play with Markdown in Java flexmark-java
Hello world in Java and Gradle
[Gradle] Build a Java project with a configuration different from the convention
Concurrency Method in Java with basic example
CICS-Run Java applications-(3) Build management with Gradle
[JaCoCo (Java Code Coverage)] Use with NetBeans
Split a string with ". (Dot)" in Java
Working with huge JSON in Java Lambda
Java automated test implementation with JUnit 5 + Gradle
Check heap usage with Java Flight Recorder
Check with Java / Kotlin that files cannot be written in UAC on Windows
Read a string in a PDF file with Java
Make SpringBoot1.5 + Gradle4.4 + Java8 + Docker environment compatible with Java11
Create a CSR with extended information in Java
Refactored GUI tools made with Java8 + JavaFX in 2016
[Gradle] Build operations often performed in Java projects
Text extraction in Java from PDF with pdfbox-2.0.8
Make Java code coverage more comfortable with Jacoco 0.8.0
Using Gradle with VS Code, build Java → run
Check static and public behavior in Java methods
Java tips-Create a Spring Boot project in Gradle
Add a project in any folder with Gradle
Practice working with Unicode surrogate pairs in Java
[JAVA] [Spring] [MyBatis] Use IN () with SQL Builder
Check method call arguments in blocks with RSpec
Encrypt / decrypt with AES256 in PHP and Java
Programming with direct sum types in Java (Neta)
Get along with Java containers in Cloud Run