Gradle Jacoco and JUnit5

后端 未结 4 764
抹茶落季
抹茶落季 2021-02-05 08:28

We just ported our unit tests to JUnit5. Realizing that this is still rather early adoption with little hints on google.

The most challenging was to get jacoco code cove

相关标签:
4条回答
  • 2021-02-05 08:59

    Thank you, so the hack now looks like this:

    project.afterEvaluate {
        def junitPlatformTestTask = project.tasks.getByName('junitPlatformTest')
    
        // configure jacoco to analyze the junitPlatformTest task
        jacoco {
            // this tool version is compatible with
            toolVersion = "0.7.6.201602180812"
            applyTo junitPlatformTestTask
        }
    
        // create junit platform jacoco task
        project.task(type: JacocoReport, "junitPlatformJacocoReport",
                {
                    sourceDirectories = files("./src/main")
                    classDirectories = files("$buildDir/classes/main")
                    executionData junitPlatformTestTask
                })
    }
    
    0 讨论(0)
  • 2021-02-05 09:06

    To get a reference to the junitPlatformTest task, another option is to implement an afterEvaluate block in the project like this:

    afterEvaluate {
      def junitPlatformTestTask = tasks.getByName('junitPlatformTest')
    
      // do something with the junitPlatformTestTask
    }
    

    See my comments on GitHub for JUnit 5 for further examples.

    0 讨论(0)
  • 2021-02-05 09:11

    You just need to add @RunWith(JUnitPlatform.class) to your package

    @RunWith(JUnitPlatform.class)
    public class ClassTest {
    }
    
    0 讨论(0)
  • 2021-02-05 09:14

    Can be also resolved with direct agent injection:

    subprojects {
       apply plugin: 'jacoco'
    
       jacoco {
            toolVersion = "0.7.9"
       }
    
       configurations {
            testAgent {
                transitive = false
            }
       }
    
       dependencies {
            testAgent("org.jacoco:org.jacoco.agent:0.7.9:runtime")
       }
    
       tasks.withType(JavaExec) {
            if (it.name == 'junitPlatformTest') {
                doFirst {
                    jvmArgs "-javaagent:${configurations.testAgent.singleFile}=destfile=${project.buildDir.name}/jacoco/test.exec"
                }
            }
        }
    }
    

    then report will be available with jacocoTestReport task

    0 讨论(0)
提交回复
热议问题