Integrate JUnit 5 tests results with Intellij test report

[亡魂溺海] 提交于 2019-12-01 20:30:37

When you execute the tests with gradle you get the results printed to the console as shown above. If you want to see the results in Idea's test report window you can simply execute the test within the IDE using the brand new built-in support: https://www.jetbrains.com/idea/whatsnew/#v2016-2-java

Hope that helps - regards matthias

As of version 2016.2 this is now fully supported. It works just like it does with junit4 test results: you do need the JUnit plugin activated (see under File menu, Settings.. item, Plugins section).

Don't use the Gradle tool window to start your test, use the IDE tools directly. These include:

  • Clicking the margin icon (looks like a green circle with a small "play" icon superimposed) and selecting "Run" or "Debug"
  • Right-clicking the class or method name and selecting "Run" or "Debug"
  • Using the "Run" menu, any menu item with "Run" or "Debug"
  • Selecting the test class in the run configuration toolbar dropdown, then clicking the green "Play" icon.

Note that currently, @TestFactory methods cannot be run this way. This can be very confusing if you've got classes with only @TestFactory methods! You can work around this by adding a dummy @Test method to classes containing @TestFactory methods, then run the class as described above. The @TestFactory methods are found correctly when the entire class is run.

As I understand it (and I'm still learning), you need to use the JUni4 based runner in order to run & view the results in Intellij (src JUnit5 docs). To do this you would need to enable the junit-platform-runner artifact in you gradle.build file. On that note, the gradle.build file examples presented seem a bit more complicated than what you have presented.

I finally got JUnit5 up in Intellij by starting with the gradle.build file shown in the junit5-gradle-consumer example, playing with it, banging my head, etc, until I got it working. There are still differences when I run the tests in the console with gradle and running them in Intellij. Sometimes Intellij will not identify a test class as a test, particularly for nested tests. But, I am able to right click on the individual classes and run them in Intellij.

Below is my hack of a gradle.build file that got things working in intellij (including what I commented out & added).

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2'
    }
}

repositories {
    mavenCentral()
}

ext.junit4Version        = '4.12'
ext.junitVintageVersion  = '4.12.0-M2'
ext.junitPlatformVersion = '1.0.0-M12'
ext.junitJupiterVersion  = '5.0.0-M2'
ext.junitPlatformConsoleVersion = '1.0.0-M2'
ext.log4JVersion         = '2.5'

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.junit.platform.gradle.plugin'

jar {
    baseName = 'x2'
    version = '1.0.0-SNAPSHOT'
}

compileTestJava {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    options.compilerArgs += '-parameters'
}

junitPlatform {
    // platformVersion '1.0.0-SNAPSHOT'
    engines {
         include 'junit-jupiter', 'junit-vintage'
        // exclude 'custom-engine'
    }
    tags {
        // include 'fast'
        // exclude 'slow'
    }
    // includeClassNamePattern '.*Test'
    // enableStandardTestTask true
    // reportsDir "build/test-results/junit-platform" // this is the default
    // logManager 'org.apache.logging.log4j.jul.LogManager'
}

dependencies {

    // JUnit Jupiter API and TestEngine implementation
    testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")

    testCompile("org.junit.platform:junit-platform-console:${junitPlatformConsoleVersion}")
    testRuntime("org.junit.platform:junit-platform-console:${junitPlatformConsoleVersion}")

    // added to run via test suite
    testCompile("org.junit.platform:junit-platform-runner:${junitPlatformConsoleVersion}")
    testRuntime("org.junit.platform:junit-platform-runner:${junitPlatformConsoleVersion}")

    // If you also want to support JUnit 3 and JUnit 4 tests
    //testCompile("junit:junit:${junit4Version}")
    //testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}")

   // testRuntime("org.apache.logging.log4j:log4j-core:${log4JVersion}")
   // testRuntime("org.apache.logging.log4j:log4j-jul:${log4JVersion}")
}

task wrapper(type: Wrapper) {
    distributionUrl = 'https://services.gradle.org/distributions/gradle-2.14.1-bin.zip'
}

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!