- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在通过 Gradle 使用 JUnit 5 平台。
我当前的构建文件有配置子句
junitPlatform {
platformVersion '1.0.0-M5'
logManager 'java.util.logging.LogManager'
enableStandardTestTask true
filters {
tags {
exclude 'integration-test'
}
packages {
include 'com.scherule.calendaring'
}
}
}
这很好用。但我还需要运行集成测试,这需要在后台构建、Docker 化和运行应用程序。所以我应该有这样的第二个配置,它只会在那时启动......如何实现这一目标?通常我会扩展测试任务来创建 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)
}
但是有没有更简单的方法呢?更一致。
最佳答案
这在带有 JUnit5 插件的 Gradle 中还没有完全支持(尽管它一直在接近)。有几种解决方法。这是我使用的那个:它有点冗长,但它做的事情与 Maven 的测试与验证相同。
Gradle 的主要和测试 sourceSets 很好。添加一个仅描述您的集成测试的新 integrationTest sourceSet。您可以使用文件名,但这可能意味着您必须调整测试 sourceSet 以跳过它当前包含的文件(在您的示例中,您希望从测试 sourceSet 中删除“.*IntegrationTest”并将其仅保留在 integrationTest 中源集)。所以我更喜欢使用与测试 sourceSet 不同的根目录名称。
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
block 一起使用:
dependencies {
// .. other stuff snipped out ..
testCompile "org.assertj:assertj-core:${assertjVersion}"
integrationTestCompile("org.springframework.boot:spring-boot-starter-test") {
exclude module: 'junit:junit'
}
}
很好!
正如您所指出的,您确实需要有一个运行集成测试的任务。您可以像示例中那样使用启动器;我只是委托(delegate)给现有的控制台运行器,以便利用简单的命令行选项。
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 和 shouldRunAfter,以确保在运行集成测试时,首先运行单元测试。为确保您的集成测试在您 ./gradlew check
时运行,您需要更新检查任务:
check {
dependsOn integrationTest
}
现在你可以像./mvnw test
一样使用./gradlew test
,像./mvnw verify一样使用
。./gradlew check
关于java - 如何在 Gradle 中的 JUnit 平台中进行两组测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45357999/
我是一名优秀的程序员,十分优秀!