Java 如何在Gradle的JUnit平台上进行两组测试
我通过Gradle使用JUnit5平台 我当前的生成文件包含配置子句Java 如何在Gradle的JUnit平台上进行两组测试,java,junit,junit5,Java,Junit,Junit5,我通过Gradle使用JUnit5平台 我当前的生成文件包含配置子句 junitPlatform { platformVersion '1.0.0-M5' logManager 'java.util.logging.LogManager' enableStandardTestTask true filters { tags { exclude 'integration-test' } pac
junitPlatform {
platformVersion '1.0.0-M5'
logManager 'java.util.logging.LogManager'
enableStandardTestTask true
filters {
tags {
exclude 'integration-test'
}
packages {
include 'com.scherule.calendaring'
}
}
}
那很好。但我还需要运行集成测试,这些测试要求应用程序在后台构建、停靠和运行。所以我应该有第二个这样的配置,只有那时才会启动。。。如何做到这一点?通常我会扩展测试任务来创建IntegrationTest任务,但它不适合JUnit平台,因为那里没有简单的任务来运行测试
我知道我可以做这样的事
task integrationTests(dependsOn: "startMyAppContainer") {
doLast {
def request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectPackage("com.scherule.calendaring"))
.filters(includeClassNamePatterns(".*IntegrationTest"))
.build()
def launcher = LauncherFactory.create()
def listener = new SummaryGeneratingListener()
launcher.registerTestExecutionListeners(listener)
launcher.execute(request)
}
finalizedBy(stopMyAppContainer)
}
但是有没有更简单的方法呢?更加一致。这在Gradle的JUnit5插件中还没有得到完全支持(尽管它越来越近了)。有几个变通办法。这是我使用的一个:有点冗长,但它与maven的test vs.verify做的事情相同 区分(单元)测试和集成测试类。 Gradle的主要和测试源集都很好。添加仅描述集成测试的新integrationTest源集。您可以使用文件名,但这可能意味着您必须调整测试源集以跳过它当前包含的文件(在您的示例中,您希望从测试源集中删除“*IntegrationTest”,并将其仅保留在IntegrationTest源集中)。因此,我更喜欢使用与测试源集的根目录名不同的根目录名
sourceSets {
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/integrationTest/java')
}
resources.srcDir file('src/integrationTest/resources')
}
}
由于我们有java插件,这非常好地创建了integrationTestCompile
和integrationTestRuntime
函数,用于依赖项
块:
dependencies {
// .. other stuff snipped out ..
testCompile "org.assertj:assertj-core:${assertjVersion}"
integrationTestCompile("org.springframework.boot:spring-boot-starter-test") {
exclude module: 'junit:junit'
}
}
很好
将集成测试添加到构建过程中的正确位置
正如您所指出的,您确实需要一个运行集成测试的任务。您可以使用示例中的启动器;我只是委托给现有的控制台运行程序,以便利用简单的命令行选项
def integrationTest = task('integrationTest',
type: JavaExec,
group: 'Verification') {
description = 'Runs integration tests.'
dependsOn testClasses
shouldRunAfter test
classpath = sourceSets.integrationTest.runtimeClasspath
main = 'org.junit.platform.console.ConsoleLauncher'
args = ['--scan-class-path',
sourceSets.integrationTest.output.classesDir.absolutePath,
'--reports-dir', "${buildDir}/test-results/junit-integrationTest"]
}
该任务定义包括dependsOn和shouldlrunafter,以确保在运行集成测试时,首先运行单元测试。要确保在执行/gradlew检查时运行集成测试,需要更新检查任务:
check {
dependsOn integrationTest
}
现在,您可以使用/gradlew test
像/mvnw test
,以及/gradlew check
像/mvnw verify
Maven能够根据类名区分单元测试和集成测试。我不熟悉Gradle,但我相信它必须有一个类似的机制。这是非常好和干净的,谢谢你,先生!如果有人想知道你是否正在对接你的应用程序,最好使用相同的容器来运行集成测试。然后,您可以在测试之后轻松地启动和停止容器。问题仍然是如何从短暂范围内分配第一个自由港。。。有人知道吗?