Spi*_*ina 84 java integration-testing build gradle source-sets
我想在我的Gradle构建(版本1.0)中添加集成测试.它们应该与我的正常测试分开运行,因为它们需要将webapp部署到localhost(它们测试该webapp).测试应该能够使用我的主要源集中定义的类.我该如何实现这一目标?
Spi*_*ina 102
我花了一段时间才弄明白,在线资源并不是很好.所以我想记录我的解决方案.
这是一个简单的gradle构建脚本,除了main和test源集外,还有一个intTest源集:
apply plugin: "java"
sourceSets {
// Note that just declaring this sourceset creates two configurations.
intTest {
java {
compileClasspath += main.output
runtimeClasspath += main.output
}
}
}
configurations {
intTestCompile.extendsFrom testCompile
intTestRuntime.extendsFrom testRuntime
}
task intTest(type:Test){
description = "Run integration tests (located in src/intTest/...)."
testClassesDir = project.sourceSets.intTest.output.classesDir
classpath = project.sourceSets.intTest.runtimeClasspath
}
Run Code Online (Sandbox Code Playgroud)
Mik*_*der 29
以下是我不使用的方法configurations{ }.
apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_6
sourceSets {
integrationTest {
java {
srcDir 'src/integrationtest/java'
}
resources {
srcDir 'src/integrationtest/resources'
}
compileClasspath += sourceSets.main.runtimeClasspath
}
}
task integrationTest(type: Test) {
description = "Runs Integration Tests"
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath += sourceSets.integrationTest.runtimeClasspath
}
Run Code Online (Sandbox Code Playgroud)
测试使用: Gradle 1.4和Gradle 1.6
chi*_*.de 16
这是曾经在2016年为Gradle 2.x/3.x编写的,已经过时了 !! 请查看Gradle 4及更高版本中记录的解决方案
总结两个旧答案(获得两个世界的最佳和最低可行性):
先说一些温暖的话:
首先,我们需要定义sourceSet:
sourceSets {
integrationTest
}
Run Code Online (Sandbox Code Playgroud)接下来我们展开sourceSetfrom test,因此我们使用test.runtimeClasspath(包括来自testAND test本身的所有依赖项)作为派生的类路径sourceSet:
sourceSets {
integrationTest {
compileClasspath += sourceSets.test.runtimeClasspath
runtimeClasspath += sourceSets.test.runtimeClasspath // ***)
}
}
Run Code Online (Sandbox Code Playgroud)
sourceSets.integrationTest.runtimeClasspath需要重新声明/扩展,但应该是无关紧要的,因为runtimeClasspath总是扩展output + runtimeSourceSet,不要得到它我们定义了一个专门用于运行集成测试的任务:
task integrationTest(type: Test) {
}
Run Code Online (Sandbox Code Playgroud)配置integrationTest测试类和类路径使用.java插件的默认值使用test sourceSet
task integrationTest(type: Test) {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
Run Code Online (Sandbox Code Playgroud)(可选)测试后自动运行
integrationTest.dependsOn test
(可选)从添加依赖check(所以当它始终运行build或check执行)
tasks.check.dependsOn(tasks.integrationTest)
Run Code Online (Sandbox Code Playgroud)(可选)添加java,资源以sourceSet支持自动检测并在IDE中创建这些"部分".即IntelliJ IDEA将自动sourceSet为每个集创建目录java和资源(如果它不存在):
sourceSets {
integrationTest {
java
resources
}
}
Run Code Online (Sandbox Code Playgroud)TL;博士
apply plugin: 'java'
// apply the runtimeClasspath from "test" sourceSet to the new one
// to include any needed assets: test, main, test-dependencies and main-dependencies
sourceSets {
integrationTest {
// not necessary but nice for IDEa's
java
resources
compileClasspath += sourceSets.test.runtimeClasspath
// somehow this redeclaration is needed, but should be irrelevant
// since runtimeClasspath always expands compileClasspath
runtimeClasspath += sourceSets.test.runtimeClasspath
}
}
// define custom test task for running integration tests
task integrationTest(type: Test) {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
tasks.integrationTest.dependsOn(tasks.test)
Run Code Online (Sandbox Code Playgroud)
指的是:
不幸的是,github.com /gradle/gradle/subprojects/docs/src/samples/java/customizedLayout/ build.gradle或.../gradle/.../withIntegrationTests/build.gradle上的示例代码似乎没有处理这个或有不同的/更复杂/对我来说无论如何都没有更清晰的解决方案!
该星云,小插件消除了样板:
apply plugin: 'nebula.facet'
facets {
integrationTest {
parentSourceSet = 'test'
}
}
Run Code Online (Sandbox Code Playgroud)
apply plugin: 'nebula.integtest'
Run Code Online (Sandbox Code Playgroud)
每个Gradle插件门户链接是:
如果您正在使用
要让 IntelliJ 将自定义源集识别为测试源根:
plugin {
idea
}
idea {
module {
testSourceDirs = testSourceDirs + sourceSets["intTest"].allJava.srcDirs
testResourceDirs = testResourceDirs + sourceSets["intTest"].resources.srcDirs
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
90595 次 |
| 最近记录: |