![](/img/trans.png)
[英]How to extract Android Gradle dependencies (implement, testImplementation…) into method in the root of project?
[英]Gradle integration test suite depending on testImplementation dependencies
我正在嘗試遷移到 Gradle 7.3 中引入的測試套件。 我想做的是將testImplementation
依賴項添加到我的集成測試中。
testing {
suites {
val test by getting(JvmTestSuite::class) {
useJUnitJupiter()
}
val integrationTest by registering(JvmTestSuite::class) {
dependencies {
implementation(project) // This adds dependencies to the prod code
// What to add to automatically use testImplementation deps?
}
...
}
}
}
您可能希望使integrationTestImplementation
配置擴展testImplementation
配置——就像默認情況下testImplementation
已經擴展implementation
一樣。 另請參閱有關配置 inheritance的文檔。
這是一個獨立的示例(使用 Gradle 7.3.2 測試):
plugins {
`java-library`
}
repositories {
mavenCentral()
}
testing {
suites {
val test by getting(JvmTestSuite::class) {
useJUnitJupiter()
dependencies {
implementation("org.assertj:assertj-core:3.21.0")
}
}
val integrationTest by registering(JvmTestSuite::class) {
dependencies {
// TODO add any integration test only dependencies here
}
}
}
}
// Here’s the bit that you’re after:
val integrationTestImplementation by configurations.getting {
extendsFrom(configurations.testImplementation.get())
}
如果您運行./gradlew dependencies --configuration integrationTestRuntimeClasspath
並配置了配置 inheritance,那么您將獲得以下 output(縮寫):
integrationTestRuntimeClasspath - Runtime classpath of source set 'integration test'.
+--- org.junit.jupiter:junit-jupiter:5.7.2
| +--- org.junit:junit-bom:5.7.2
| | …
| +--- org.junit.jupiter:junit-jupiter-api:5.7.2
| | …
| +--- org.junit.jupiter:junit-jupiter-params:5.7.2
| | …
| \--- org.junit.jupiter:junit-jupiter-engine:5.7.2
| …
\--- org.assertj:assertj-core:3.21.0
但是,如果您在沒有配置 inheritance 的情況下運行相同的命令,那么您將獲得以下 output(縮寫) - 請注意缺少的org.assertj:assertj-core:3.21.0
依賴項:
integrationTestRuntimeClasspath - Runtime classpath of source set 'integration test'.
\--- org.junit.jupiter:junit-jupiter:5.7.2
+--- org.junit:junit-bom:5.7.2
| …
+--- org.junit.jupiter:junit-jupiter-api:5.7.2
| …
+--- org.junit.jupiter:junit-jupiter-params:5.7.2
| …
\--- org.junit.jupiter:junit-jupiter-engine:5.7.2
根據答案評論中的要求,這里還有一種方法可以使單元測試套件中的測試數據 class 可用於集成測試:
sourceSets.named("integrationTest") {
java {
val sharedTestData = project.objects.sourceDirectorySet("testData",
"Shared test data")
sharedTestData.srcDir("src/test/java")
sharedTestData.include("com/example/MyData.java")
source(sharedTestData)
}
}
我想出了以下解決方案
testing {
suites {
val test by getting(JvmTestSuite::class) {
useJUnitJupiter()
}
val integrationTest by registering(JvmTestSuite::class) {
useJUnitJupiter()
dependencies {
implementation(project)
// add testImplementation dependencies
configurations.testImplementation {
dependencies.forEach(::implementation)
}
}
sources {
java {
//...
}
}
targets {
all {
testTask.configure {
shouldRunAfter(test)
}
}
}
}
}
}
我是 Gradle 開發人員,目前正在研究這個孵化功能。
需要記住的一件事是,雖然目前為每個測試套件創建的配置的名稱是基於相應的sourceSet
的名稱,它與測試套件本身的名稱相匹配,但這些名稱應該被視為實現細節。 測試套件的主要目標之一是在這些細節上構建一個有用的抽象層,並允許您在沒有此類管道的詳細知識的情況下對其進行配置。
將配置與extendsFrom
連接在一起違背了這個意圖,任何類型的按名稱檢索也是如此,並且應該被視為反模式。
更好的選擇包括使用suites.withType(JvmTestSuite).configureEach { suite ->... }
或創建一個包含您想要的配置的Closure
並使用它來配置感興趣的測試套件,如下所示:
testing {
suites {
def applyMockitoAndJupiter = { suite ->
suite.useJUnitJupiter()
suite.dependencies {
implementation('org.mockito:mockito-junit-jupiter:4.6.1')
}
}
/* This is the equivalent of:
test {
applyMockitoAndJupiter(this)
}
*/
test(applyMockitoAndJupiter)
/* This is the equivalent of:
integrationTest(JvmTestSuite)
applyMockitoAndJupiter(integrationTest)
*/
integrationTest(JvmTestSuite, applyMockitoAndJupiter)
}
}
我們正在努力擴展測試套件和每個套件的dependencies
塊提供的功能,並使文檔也更明確地說明這一點。 您可以在 Gradle 文檔的未來版本中查看此建議的預覽(可能會進一步更改) - 您必須以訪客身份登錄。
這就是我用來不為test
和customTest
任務重寫兩次相同的依賴項的方法。 也許不推薦@Tom Tresansky,但我覺得它很方便:
configurations {
customTestImplementation {
extendsFrom testImplementation
}
customTestCompileOnly {
extendsFrom testCompileOnly
}
customTestAnnotationProcessor {
extendsFrom annotationProcessor
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.