【问题标题】:Integrate JUnit 5 tests results with Intellij test report将 JUnit 5 测试结果与 Intellij 测试报告集成
【发布时间】:2016-11-29 06:55:59
【问题描述】:

我的 build.gradle 是这样配置的:

apply plugin: 'java'

compileTestJava {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
}

repositories {
    mavenCentral()
}

dependencies {
    testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M1")
    testRuntime("org.junit.vintage:junit-vintage-engine:5.0.0-M1")
}

还有一个像这样的简单测试:

public class JUnit5Test {
    @Test
    void test() {

    }
}

当我执行测试时,我会在控制台中看到:

Test run finished after 76 ms
[         1 tests found     ]
[         0 tests skipped   ]
[         1 tests started   ]
[         0 tests aborted   ]
[         1 tests successful]
[         0 tests failed    ]

BUILD SUCCESSFUL

但是测试报告中什么都没有:

我做错了什么?如何将 JUnit 5 结果集成到测试报告窗口中?

我正在使用 Intellij 2016.2

【问题讨论】:

    标签: java intellij-idea junit5


    【解决方案1】:

    当您使用 gradle 执行测试时,您会得到打印到控制台的结果,如上所示。如果您想在 Idea 的测试报告窗口中查看结果,您可以使用全新的内置支持在 IDE 中简单地执行测试: https://www.jetbrains.com/idea/whatsnew/#v2016-2-java

    希望对您有所帮助 - 关于 matthias

    【讨论】:

    • 好的,但是怎么用呢?
    【解决方案2】:

    从 2016.2 版开始,现在是 fully supported。它的工作原理与 junit4 测试结果一样:您确实需要激活 JUnit 插件(请参阅文件菜单下的设置.. 项,插件部分)。

    不要使用 Gradle 工具窗口开始测试,直接使用 IDE 工具。其中包括:

    • 单击边距图标(看起来像一个绿色圆圈,上面叠加了一个小的“播放”图标)并选择“运行”或“调试”
    • 右键单击类或方法名称并选择“运行”或“调试”
    • 使用“运行”菜单,任何带有“运行”或“调试”的菜单项
    • 在运行配置工具栏下拉菜单中选择测试类,然后点击绿色的“播放”图标。

    请注意,目前,@TestFactory 方法不能以这种方式运行。如果您的课程仅包含 @TestFactory 方法,这可能会非常令人困惑!您可以通过向包含@TestFactory 方法的类添加一个虚拟的@Test 方法来解决此问题,然后如上所述运行该类。 @TestFactory 方法是在整个类运行时正确找到的。

    【讨论】:

      【解决方案3】:

      据我了解(我仍在学习),您需要使用基于 JUni4 的运行器才能在 Intellij 中运行和查看结果 (src JUnit5 docs)。为此,您需要在 gradle.build 文件中启用 junit-platform-runner 工件。在那一点上,所提供的 gradle.build 文件示例似乎比您所提供的要复杂一些。

      我终于在 Intellij 中安装了 JUnit5,从 junit5-gradle-consumer 示例中显示的 gradle.build 文件开始,使用它,敲打我的头等,直到我让它工作。当我使用 gradle 在控制台中运行测试并在 Intellij 中运行它们时,仍然存在差异。有时 Intellij 不会将测试类识别为测试,尤其是对于嵌套测试。但是,我可以右键单击各个类并在 Intellij 中运行它们。

      下面是我对 gradle.build 文件的破解,它可以在 intellij 中运行(包括我注释掉和添加的内容)。

      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'
      }
      

      希望这会有所帮助。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-26
      • 2018-07-29
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      相关资源
      最近更新 更多