PHP*_*ate 33 junit gradle build.gradle junit5
在成功运行JUnit 4测试后,我正在尝试将JUnit 5与Gradle一起使用.
预期的结果: JUnit 4测试在输出中给出了一个很好的'传递',并且输入了一个html报告build/reports/tests.
实际结果: JUnit 5测试如下所示不输出任何内容(...) build succesful,虽然我知道测试实际上没有运行,因为没有传递/跳过/失败的测试日志输出,并且fail在测试中放置一个使构建成功.
在很多我认为主要是不相关的输出之间运行gradle test --info收益率Skipping task ':testClasses' as it has no actions..令人惊讶的是,它也说Executing task ':test'和Generating HTML test report... Finished generating test html resultsxml中的类似build/test-results/test,而xml没有生成,html显示没有测试运行且没有错误,测试确实没有运行.
我认为非常有趣的是gradle test --debug收益率
[TestEventLogger] Gradle Test Run :test STARTED
[org.gradle.api.internal.tasks.testing.junit.JUnitDetector] test-class-
scan : failed to scan parent class java/lang/Object, could not find the class file
[TestEventLogger]
[TestEventLogger] Gradle Test Run :test PASSED
Run Code Online (Sandbox Code Playgroud)
而我唯一的测试包含
fail("test fails");
Run Code Online (Sandbox Code Playgroud)
我认为这很奇怪!
我的构建文件是
apply plugin: 'java'
test {
dependsOn 'cleanTest' // run tests every time
}
sourceSets {
main {
java {
srcDirs 'src'
}
}
test {
java {
srcDirs 'test'
}
}
}
repositories {
mavenCentral()
}
dependencies {
// when using this, it worked with a junit 4 test
// testCompile 'junit:junit:4.10'
// this should be needed for junit 5 (using M4 is required since IJ 2017.1.2
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
}
test {
testLogging {
events "passed", "skipped", "failed"
}
}
Run Code Online (Sandbox Code Playgroud)
我的考试是
package mypackage;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class HelloWorldTest {
@Test
public void testHelloWorld(){
assertEquals(2, 1+1, "message");
}
}
Run Code Online (Sandbox Code Playgroud)
我的文件夹结构是,使用包mypackage,
java-template-project
--- src
--- mypackage
--- HelloWorld.java
--- test
--- mypackage
--- HelloWorldTest.java
Run Code Online (Sandbox Code Playgroud)
在我使用的IntelliJ 2017.1.3中,模块结构如下所示
java-template-project
--- java-template-project_main
--- src/mypackage
--- HelloWorld(.java)
--- java-template-project_test
--- test/mypackage
--- HelloWorldTest(.java)
Run Code Online (Sandbox Code Playgroud)
因为Gradle现在想要源和测试在他们自己的包中.
我尝试了什么
显然这不是关于这个主题的第一个问题,我发现的所有相关问题都是
但是你可以看到这是针对旧版本的IntelliJ,并且我已经根据其中一个答案使用了IJ 2016.3.3及更高版本的语法,在一个JUnit依赖行中,所以应该没问题.
使用gradle从intellij中的JUnit 4升级到JUnit 5
回到上面的问题链接,并链接到这个使用与上述问题相同的Jetbrains博客.还链接到:
将JUnit 5测试结果与Intellij测试报告集成 这个问题在问题中也显示为依赖性
testRuntime("org.junit.vintage:junit-vintage-engine:5.0.0-M1")
Run Code Online (Sandbox Code Playgroud)
当我在IntelliJ中运行TestCase时,为什么JUnit Jupiter和JUnit Vintage分离? 好吧,当我运行它时,输出显示它找不到这个版本但是根据Maven Repository这个是JUnit 5:
testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")
Run Code Online (Sandbox Code Playgroud)
答案中注意到您可以在IntelliJ中运行测试,因为更高版本具有JUnit 5支持.我知道,当我在IntelliJ中运行时,测试运行正常.但我想使用Gradle(和需要依赖管理的Travis).
如何在junit 5 gradle测试报告中捕获stdout/stderr?
我试过用
testCompile("org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3")
testCompile("org.junit.jupiter:junit-jupiter-engine:5.0.0-M3")
Run Code Online (Sandbox Code Playgroud)
但结果没有改变.
我的模板项目位于https://github.com/PHPirates/java-template-project,但此问题应包含所有必要的信息.
PHP*_*ate 40
正如Gradle 4.6以后的GitHub问题所指出的那样,支持JUnit 5!官方发布说明4.6(在编辑最新版本时,但请查看下面的链接)在docs.gradle.org.旧设置仍然有效,但使用此设置可使构建文件更加清晰.
更新Gradle
首先,确保您使用的是最新的Gradle版本,请在GitHub版本中查看最新版本.如果是例如4.6,则在项目位置的终端中运行,build.gradle或确保在gradlew wrapper --gradle-version=4.6文件中更新此行:gradle/wrapper/gradle-wrapper.properties.
如何使用内置的JUnit 5
然后使用java文件,目录结构等来自distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip文件的问题(使用新build.gradle块)
plugins {
id 'java'
}
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.0.3'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.0.3'
}
// These lines can be removed when you use the default directories src/main/kotlin and src/test/kotlin
sourceSets {
main.java.srcDirs += 'src'
main.resources.srcDirs += 'src'
test.java.srcDirs += 'test'
test.resources.srcDirs += 'test'
}
// Java target version
sourceCompatibility = 1.8
test {
// Enable JUnit 5 (Gradle 4.6+).
useJUnitPlatform()
// Always run tests, even when nothing changed.
dependsOn 'cleanTest'
// Show test results.
testLogging {
events "passed", "skipped", "failed"
}
}
Run Code Online (Sandbox Code Playgroud)
PS对于绝对最小版本,请参阅Ray的答案.
Ral*_*ert 22
您需要两个JUnit版本的引擎,并且需要应用JUnit平台gradle插件.我在你的gradle文件中没有看到.这是一个执行JUnit 4和5的工作gradle构建:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath ("org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4")
}
}
apply plugin: 'org.junit.platform.gradle.plugin'
...
dependencies {
...
testCompile("junit:junit:4.12")
testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0-M4")
// Enable use of the JUnitPlatform Runner within the IDE
testCompile("org.junit.platform:junit-platform-runner:1.0.0-M4")
}
junitPlatform {
details 'tree'
}
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅JUnit doc表单.
Ray*_*yek 10
只是添加到知识库,我只需要使用gradle 4.7:
apply plugin: 'java'
repositories {
jcenter()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.1'
}
test {
useJUnitPlatform()
}
Run Code Online (Sandbox Code Playgroud)
由于github 问题,JUnit 5的内置支持计划在Gradle 4.6中进行
因此从gradle 4.6开始,您的预期结果必须与实际结果相同。
预期结果:JUnit 4测试在输出中给出了很好的“通过”,并在中给出了html报告
build/reports/tests。
UPD:
gradle 4.6-rc-1于2018年2月16日发布,该版本提供了对junit 5的内置支持。
要启用junit 5支持,您需要更新gradle包装器:
gradle wrapper --gradle-version=4.6-rc-1
Run Code Online (Sandbox Code Playgroud)
并仅添加一行到build.gradle:
test {
useJUnitPlatform()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25554 次 |
| 最近记录: |