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.
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
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.
Various options have been added to the new Java syntax, such as whether to cover it. Reference article
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
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
In the reference sample, the Java version of targetCompatibility was also described, so I added the following as well.
targetCompatibility = 10
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
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
Add Codecov token to Wercker build phase environment variables
Open the target application screen of Wercker
Open the Workflows tab
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
Open the <> Environment tab
Enter the above environment variable name CODECOV_TOKEN in Key. --If you use any other environment variable name, use that name.
Enter the token in Value
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.
Click the Add button to complete the addition
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 in the root directory of the repository
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
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.
Recommended Posts