我正在通过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)
}
但有更简单的方法吗?更一致.
解决方法:
Gradle中还没有完全支持JUnit5插件(虽然它一直在变得越来越近).有几种解决方法.这是我使用的那个:它有点冗长,但它与maven的测试与验证完全相同.
区分(单元)测试和集成测试类.
Gradle的主要和测试源集合都很好.添加一个仅描述集成测试的新IntegrationTest源集.您可以使用文件名,但这可能意味着您必须调整测试源集以跳过它当前包含的文件(在您的示例中,您要从测试源集中删除“.* IntegrationTest”并仅将其保留在IntegrationTest中sourceSet).所以我更喜欢使用与测试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 {
// .. 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和shouldRunAfter,以确保在运行集成测试时,首先运行单元测试.要确保在./gradlew检查时运行集成测试,您需要更新检查任务:
check {
dependsOn integrationTest
}
现在你使用./mvnw测试的./gradlew测试,和./mvnw验证的./gradlew检查.
标签:java,junit,junit5 来源: https://codeday.me/bug/20190611/1217123.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。