知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
https://docs.gradle.org/current/userguide/jacoco_plugin.html
plugins 中加入 id 'jacoco'
/* * This file was generated by the Gradle 'init' task. * * This generated file contains a sample Java Library project to get you started. * For more details take a look at the Java Libraries chapter in the Gradle * user guide available at https://docs.gradle.org/5.0/userguide/java_library_plugin.html */ plugins { // Apply the java-library plugin to add support for Java Library id 'java-library' // id 'java' id 'eclipse' // optional (to generate Eclipse project files) id 'idea' // optional (to generate IntelliJ IDEA project files) id 'jacoco' } repositories { // Use jcenter for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. jcenter() //mavenCentral() } dependencies { testCompile('org.junit.jupiter:junit-jupiter-api:5.3.2') testCompile('org.junit.jupiter:junit-jupiter-params:5.3.2') testRuntime('org.junit.jupiter:junit-jupiter-engine:5.3.2') testCompile 'io.rest-assured:rest-assured:3.2.0' testCompile 'io.rest-assured:json-schema-validator:3.2.0' compile 'io.rest-assured:rest-assured:3.2.0' compile 'io.rest-assured:json-path:3.2.0' compile 'io.rest-assured:xml-path:3.2.0' } test { useJUnitPlatform() testLogging { events "passed", "skipped", "failed" } }
運行 gradlew build jacocoTestReport 生成測試報告
neo@MacBook-Pro ~/workspace/junit5 % ./gradlew build jacocoTestReport BUILD SUCCESSFUL in 5s 6 actionable tasks: 2 executed, 4 up-to-date neo@MacBook-Pro ~/workspace/junit5 % find build/reports/jacoco build/reports/jacoco build/reports/jacoco/test build/reports/jacoco/test/html build/reports/jacoco/test/html/index.html build/reports/jacoco/test/html/jacoco-sessions.html build/reports/jacoco/test/html/jacoco-resources build/reports/jacoco/test/html/jacoco-resources/branchnc.gif build/reports/jacoco/test/html/jacoco-resources/sort.js build/reports/jacoco/test/html/jacoco-resources/greenbar.gif build/reports/jacoco/test/html/jacoco-resources/sort.gif build/reports/jacoco/test/html/jacoco-resources/source.gif build/reports/jacoco/test/html/jacoco-resources/report.gif build/reports/jacoco/test/html/jacoco-resources/prettify.js build/reports/jacoco/test/html/jacoco-resources/branchpc.gif build/reports/jacoco/test/html/jacoco-resources/branchfc.gif build/reports/jacoco/test/html/jacoco-resources/class.gif build/reports/jacoco/test/html/jacoco-resources/prettify.css build/reports/jacoco/test/html/jacoco-resources/report.css build/reports/jacoco/test/html/jacoco-resources/group.gif build/reports/jacoco/test/html/jacoco-resources/package.gif build/reports/jacoco/test/html/jacoco-resources/redbar.gif build/reports/jacoco/test/html/jacoco-resources/up.gif build/reports/jacoco/test/html/jacoco-resources/session.gif build/reports/jacoco/test/html/jacoco-resources/down.gif build/reports/jacoco/test/html/jacoco-resources/method.gif build/reports/jacoco/test/html/jacoco-resources/bundle.gif build/reports/jacoco/test/html/net.coding.api.task build/reports/jacoco/test/html/net.coding.api.task/Task.java.html build/reports/jacoco/test/html/net.coding.api.task/index.source.html build/reports/jacoco/test/html/net.coding.api.task/index.html build/reports/jacoco/test/html/net.coding.api.task/Task.html build/reports/jacoco/test/html/junit5 build/reports/jacoco/test/html/junit5/Library.java.html build/reports/jacoco/test/html/junit5/index.source.html build/reports/jacoco/test/html/junit5/index.html build/reports/jacoco/test/html/junit5/UnitTest.java.html build/reports/jacoco/test/html/junit5/Library.html build/reports/jacoco/test/html/junit5/UnitTest.html build/reports/jacoco/test/html/junit5/Config.html build/reports/jacoco/test/html/junit5/Config.java.html build/reports/jacoco/test/html/com.example.project build/reports/jacoco/test/html/com.example.project/index.source.html build/reports/jacoco/test/html/com.example.project/index.html build/reports/jacoco/test/html/com.example.project/Calculator.java.html build/reports/jacoco/test/html/com.example.project/Calculator.html
如果你希望自動執行,不需要攜帶 jacocoTestReport 任務,添加下面代碼到 build.gradle 檔案最下面
test.finalizedBy(project.tasks.jacocoTestReport)
排除 class
jacocoTestReport { afterEvaluate { classDirectories = files(classDirectories.files.collect { fileTree(dir: it, exclude: [ 'io/reflectoring/coverage/ignored/**', 'io/reflectoring/coverage/part/**' ]) }) } }