Skip to content

Commit 9bad031

Browse files
authored
Introduce functional tests (#26)
* Introduce functional unit tests akin to the JUnit 5 Java plugin This adds "FunctionalSpec" classes that verify the correct execution of JUnit 5 tests in different scenarios. We've had some issues with test discovery in the past, which go beyond the "pure" plugin tests that assert the correct configuration and behavior. These new tests create virtual projects & run their unit tests with GradleRunner, then assert if everything went as expected. These tests are detecting the incorrect behavior for issues like #25, which are related to a bug in the Kotlin copy task, for instance. * #25: Fix destination of copied Kotlin classes for AGP3
1 parent 9de6cde commit 9bad031

File tree

10 files changed

+603
-21
lines changed

10 files changed

+603
-21
lines changed

android-junit5/build.gradle

+55-16
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,6 @@ processTestResources {
6464
}
6565
}
6666

67-
// https://docs.gradle.org/current/userguide/test_kit.html#sub:test-kit-classpath-injection
68-
// Write the plugin's classpath to a file to share with the tests
69-
task createTestClasspathManifest {
70-
def outputDir = file("$buildDir/resources/test")
71-
72-
inputs.files sourceSets.main.runtimeClasspath
73-
outputs.dir outputDir
74-
75-
doLast {
76-
outputDir.mkdirs()
77-
file("$outputDir/plugin-classpath.txt").text = sourceSets.main.runtimeClasspath.join("\n")
78-
}
79-
}
80-
81-
processTestResources.finalizedBy createTestClasspathManifest
82-
8367
// ------------------------------------------------------------------------------------------------
8468
// Test Setup
8569
//
@@ -96,11 +80,27 @@ final def jacocoData3x = "$buildDir/jacoco/testAgp3x.exec"
9680

9781
configurations {
9882
testAgp2xCompile {
83+
description = "Dependencies used for compiling tests using Android Gradle Plugin 2.x"
9984
extendsFrom configurations.testCompile
10085
}
10186
testAgp3xCompile {
87+
description = "Dependencies used for compiling tests using Android Gradle Plugin 3.x"
10288
extendsFrom configurations.testCompile
10389
}
90+
functionalTest {
91+
description = "Local dependencies used for compiling & running tests source code " +
92+
"inside of Gradle functional tests"
93+
}
94+
functionalTestAgp2x {
95+
description = "Local dependencies used for compiling & running tests source code " +
96+
"inside of Gradle functional tests with Android Gradle Plugin 2.x"
97+
extendsFrom configurations.functionalTest
98+
}
99+
functionalTestAgp3x {
100+
description = "Local dependencies used for compiling & running tests source code " +
101+
"inside of Gradle functional tests with Android Gradle Plugin 3.x"
102+
extendsFrom configurations.functionalTest
103+
}
104104
}
105105

106106
sourceSets {
@@ -148,6 +148,37 @@ task testAgp3x(type: Test) {
148148
// Combine all tests when executing the main JUnit task
149149
tasks.getByName("test").dependsOn(testAgp2x, testAgp3x)
150150

151+
// https://docs.gradle.org/current/userguide/test_kit.html#sub:test-kit-classpath-injection
152+
// Write the plugin's classpath to a file to share with the tests
153+
task createTestClasspathManifests {
154+
description =
155+
"Generate classpath manifests for functional tests so that they can reference locally" +
156+
" built libraries for use with Gradle Test Kit"
157+
def outputDir = file("$buildDir/resources/test")
158+
159+
inputs.files sourceSets.main.runtimeClasspath
160+
inputs.files sourceSets.testAgp2x.runtimeClasspath
161+
inputs.files sourceSets.testAgp3x.runtimeClasspath
162+
inputs.files configurations.testAgp2xCompile
163+
inputs.files configurations.testAgp3xCompile
164+
outputs.dir outputDir
165+
166+
doLast {
167+
outputDir.mkdirs()
168+
file("$outputDir/plugin-classpath.txt").text = sourceSets.main.runtimeClasspath.join("\n")
169+
file("$outputDir/plugin-2x-classpath.txt").text =
170+
sourceSets.testAgp2x.runtimeClasspath.join("\n")
171+
file("$outputDir/plugin-3x-classpath.txt").text =
172+
sourceSets.testAgp3x.runtimeClasspath.join("\n")
173+
file("$outputDir/functional-test-compile-2x-classpath.txt").text =
174+
configurations.functionalTestAgp2x.files.join("\n")
175+
file("$outputDir/functional-test-compile-3x-classpath.txt").text =
176+
configurations.functionalTestAgp3x.files.join("\n")
177+
}
178+
}
179+
180+
processTestResources.finalizedBy createTestClasspathManifests
181+
151182
// ------------------------------------------------------------------------------------------------
152183
// Dependency Definitions
153184
// ------------------------------------------------------------------------------------------------
@@ -159,11 +190,19 @@ dependencies {
159190
compileOnly "com.android.tools.build:gradle:$ANDROID_PLUGIN_2X_VERSION"
160191

161192
testCompile "junit:junit:$JUNIT4_VERSION"
193+
testCompile "commons-lang:commons-lang:2.6"
162194
testCompile "org.spockframework:spock-core:$SPOCK_VERSION"
163195
testCompile "org.jetbrains.kotlin:kotlin-gradle-plugin:$KOTLIN_VERSION"
164196
testCompileOnly "com.android.tools.build:gradle:$ANDROID_PLUGIN_3X_VERSION"
165197
testAgp2xCompile "com.android.tools.build:gradle:$ANDROID_PLUGIN_2X_VERSION"
166198
testAgp3xCompile "com.android.tools.build:gradle:$ANDROID_PLUGIN_3X_VERSION"
199+
200+
functionalTest "junit:junit:$JUNIT4_VERSION"
201+
functionalTest "org.junit.jupiter:junit-jupiter-api:$JUNIT_JUPITER_VERSION"
202+
functionalTest "org.junit.jupiter:junit-jupiter-engine:$JUNIT_JUPITER_VERSION"
203+
functionalTest "org.junit.platform:junit-platform-console:$JUNIT_PLATFORM_VERSION"
204+
functionalTestAgp2x "com.android.tools.build:gradle:$ANDROID_PLUGIN_2X_VERSION"
205+
functionalTestAgp3x "com.android.tools.build:gradle:$ANDROID_PLUGIN_3X_VERSION"
167206
}
168207

169208
// ------------------------------------------------------------------------------------------------

android-junit5/src/main/groovy/de/mannodermaus/gradle/plugins/android_junit5/kotlin/AndroidJUnit5CopyKotlin.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class AndroidJUnit5CopyKotlin extends Copy {
4545
@Override
4646
void execute(AndroidJUnit5CopyKotlin copyTask) {
4747
copyTask.from "$project.buildDir/tmp/kotlin-classes/${variant.name}UnitTest"
48-
copyTask.into "$project.buildDir/intermediates/classes/test/$variant.name"
48+
copyTask.into "$project.buildDir/intermediates/classes/test/$variant.flavorName/$variant.buildType.name"
4949
copyTask.group = TASK_GROUP
5050

5151
testTask.dependsOn copyTask

0 commit comments

Comments
 (0)