将JUnit 5测试结果与Intellij测试报告集成
我的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
使用gradle执行测试时,您将结果打印到控制台,如上所示。 如果您想在Idea的测试报告窗口中查看结果,您只需使用全新的内置支持在IDE中执行测试: https : //www.jetbrains.com/idea/whatsnew/#v2016-2-java
希望有所帮助 – 关于马赛厄斯
从版本2016.2开始,现在完全支持 。 它就像使用junit4测试结果一样工作:你需要激活JUnit插件(参见File菜单,Settings .. item,Plugins部分)。
不要使用Gradle工具窗口开始测试,直接使用IDE工具。 这些包括:
- 单击边距图标(看起来像一个叠加了小“播放”图标的绿色圆圈)并选择“运行”或“调试”
- 右键单击类或方法名称,然后选择“运行”或“调试”
- 使用“运行”菜单,任何带有“运行”或“调试”的菜单项
- 在运行配置工具栏下拉列表中选择测试类,然后单击绿色的“播放”图标。
注意,目前, @TestFactory
方法不能以这种方式运行。 如果您只有@TestFactory
方法的类,这可能会非常混乱! 您可以通过向包含@TestFactory
方法的类添加虚拟@Test
方法来解决此问题,然后按上述方法运行该类。 运行整个类时,可以正确找到@TestFactory
方法。
据我了解(我还在学习),您需要使用基于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文件的hack,它在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' }
希望这可以帮助。
- Stackdriver Debug不捕获快照
- IntelliJ社区版 – 如何导入Eclipse项目?
- 如何添加@NotNull注释的运行时处理
- 所有子包的IntelliJ IDEA @ParametersAreNonnullByDefault
- IntelliJ IDEA控制台中的unicode字符显示为问号
- 如何让IntellijIDEA忽略正在进行中的类文件?
- 启动创意时出错。 无法加载JVM DLL C:\ Program Files \ Java \ jdk1.8.0_112
- Intellij IDEA:如何更改组织导入function
- 如何使用IntelliJ将外部库的源和javadoc添加到gradle中?