Skip to content

Commit 620a3be

Browse files
committed
Add instrumentation test APIs to unit test configurations as well
Same logic as with androidTest: Add Compose only if it's detected
1 parent 5635786 commit 620a3be

File tree

5 files changed

+121
-41
lines changed

5 files changed

+121
-41
lines changed

plugin/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Change Log
88
- Update Jacoco & instrumentation test DSLs of the plugin to use Gradle Providers for their input parameters (e.g. `instrumentationTests.enabled.set(true)` instead of `instrumentationTests.enabled = true`)
99
- Removed deprecated `integrityCheckEnabled` flag from the plugin DSL's instrumentation test options
1010
- Allow opt-in usage of extension library via the plugin's DSL
11-
- Allow autoconfiguration of compose library if Compose is used in the androidTest dependency list
11+
- Allow autoconfiguration of instrumentation libraries if Compose or JUnit 5 are found among the test/androidTest dependency lists
1212
- Decouple discovery of instrumentation tests from Jupiter, allowing non-Jupiter test engines to be discovered as well
1313
- Update lifecycle of instrumentation runner params to only be set once, instead of once per test
1414
- Properly reported disabled dynamic tests to Android instrumentation

plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/AndroidJUnitPlatformExtension.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public abstract class AndroidJUnitPlatformExtension @Inject constructor(
9898
enabled.convention(true)
9999
version.convention(Libraries.instrumentationVersion)
100100
includeExtensions.convention(false)
101+
useConfigurationParameters.convention(true)
101102
}
102103

103104
public fun instrumentationTests(action: Action<InstrumentationTestOptions>) {

plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/dsl/InstrumentationTestOptions.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@ public abstract class InstrumentationTestOptions {
2727
*/
2828
@get:Input
2929
public abstract val includeExtensions: Property<Boolean>
30+
31+
/**
32+
* Whether to use configuration parameters configured via the plugin DSL
33+
* for instrumentation tests, too.
34+
*/
35+
@get:Input
36+
public abstract val useConfigurationParameters: Property<Boolean>
3037
}

plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/internal/configureJUnit5.kt

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private fun AndroidJUnitPlatformExtension.prepareVariantDsl(variant: Variant) {
9292
}
9393
}
9494

95-
private fun prepareUnitTests(project: Project, android: AndroidExtension) {
95+
private fun AndroidJUnitPlatformExtension.prepareUnitTests(project: Project, android: AndroidExtension) {
9696
// Add default ignore rules for JUnit 5 metadata files to the packaging options of the plugin,
9797
// so that consumers don't need to do this explicitly
9898
val options = excludedPackagingOptions()
@@ -106,6 +106,8 @@ private fun prepareUnitTests(project: Project, android: AndroidExtension) {
106106
// Fall back to the old DSL when this happens
107107
options.forEach(project.android.packagingOptions::exclude)
108108
}
109+
110+
attachDependencies(project, "testImplementation", includeRunner = false)
109111
}
110112

111113
private fun AndroidJUnitPlatformExtension.prepareInstrumentationTests(project: Project, android: AndroidExtension) {
@@ -123,29 +125,47 @@ private fun AndroidJUnitPlatformExtension.prepareInstrumentationTests(project: P
123125
.joinToString(",")
124126
}
125127

126-
val version = instrumentationTests.version.get()
127-
128-
project.dependencies.add(
129-
"androidTestImplementation",
130-
"${Libraries.instrumentationCore}:$version"
131-
)
132-
project.dependencies.add(
133-
"androidTestRuntimeOnly",
134-
"${Libraries.instrumentationRunner}:$version"
135-
)
136-
137-
if (instrumentationTests.includeExtensions.get()) {
138-
project.dependencies.add(
139-
"androidTestImplementation",
140-
"${Libraries.instrumentationExtensions}:$version"
141-
)
128+
// Copy over configuration parameters to instrumentation tests
129+
if (instrumentationTests.useConfigurationParameters.get()) {
130+
val instrumentationParams = android.defaultConfig.testInstrumentationRunnerArguments
131+
.getAsList("configurationParameters")
132+
.toMutableList()
133+
134+
this.configurationParameters.get().forEach { (key, value) ->
135+
instrumentationParams.add("$key=$value")
136+
}
137+
138+
android.defaultConfig.testInstrumentationRunnerArguments["configurationParameters"] =
139+
instrumentationParams.joinToString(",")
142140
}
143141

144-
if (project.usesComposeIn("androidTestImplementation")) {
145-
project.dependencies.add(
146-
"androidTestImplementation",
147-
"${Libraries.instrumentationCompose}:$version"
148-
)
142+
attachDependencies(project, "androidTestImplementation", includeRunner = true)
143+
}
144+
145+
private fun AndroidJUnitPlatformExtension.attachDependencies(
146+
project: Project,
147+
configurationName: String,
148+
includeRunner: Boolean,
149+
) {
150+
if (project.usesJUnitJupiterIn(configurationName)) {
151+
val version = instrumentationTests.version.get()
152+
153+
project.dependencies.add(configurationName, "${Libraries.instrumentationCore}:$version")
154+
155+
if (includeRunner) {
156+
project.dependencies.add(
157+
configurationName.replace("Implementation", "RuntimeOnly"),
158+
"${Libraries.instrumentationRunner}:$version",
159+
)
160+
}
161+
162+
if (instrumentationTests.includeExtensions.get()) {
163+
project.dependencies.add(configurationName, "${Libraries.instrumentationExtensions}:$version")
164+
}
165+
166+
if (project.usesComposeIn(configurationName)) {
167+
project.dependencies.add(configurationName, "${Libraries.instrumentationCompose}:$version")
168+
}
149169
}
150170
}
151171

plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/plugin/InstrumentationSupportTests.kt

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class InstrumentationSupportTests {
3232

3333
@Test
3434
fun `add the RunnerBuilder`() {
35-
project.addJUnitJupiterApi()
35+
project.addJUnitJupiterApi("androidTest")
3636
project.evaluate()
3737

3838
assertThat(project.android.defaultConfig.testInstrumentationRunnerArguments["runnerBuilder"])
@@ -41,7 +41,7 @@ class InstrumentationSupportTests {
4141

4242
@Test
4343
fun `maintain any existing RunnerBuilder`() {
44-
project.addJUnitJupiterApi()
44+
project.addJUnitJupiterApi("androidTest")
4545
project.android.defaultConfig.testInstrumentationRunnerArguments["runnerBuilder"] = "something.else"
4646
project.evaluate()
4747

@@ -51,7 +51,7 @@ class InstrumentationSupportTests {
5151

5252
@Test
5353
fun `do not add the RunnerBuilder when disabled`() {
54-
project.addJUnitJupiterApi()
54+
project.addJUnitJupiterApi("androidTest")
5555
project.junitPlatform.instrumentationTests.enabled.set(false)
5656
project.evaluate()
5757

@@ -65,11 +65,52 @@ class InstrumentationSupportTests {
6565
assertThat(project.android.defaultConfig.testInstrumentationRunnerArguments["runnerBuilder"]).isNull()
6666
}
6767

68+
/* Configuration parameters */
69+
70+
@Test
71+
fun `copy configuration parameters to test runner arguments`() {
72+
project.addJUnitJupiterApi("androidTest")
73+
with (project.junitPlatform) {
74+
configurationParameter("my.parameter1", "true")
75+
configurationParameter("my.parameter2", "1234")
76+
}
77+
project.evaluate()
78+
79+
assertThat(project.android.defaultConfig.testInstrumentationRunnerArguments["configurationParameters"])
80+
.isEqualTo("my.parameter1=true,my.parameter2=1234")
81+
}
82+
83+
@Test
84+
fun `do not copy configuration parameters if disabled via dsl`() {
85+
project.addJUnitJupiterApi("androidTest")
86+
with (project.junitPlatform) {
87+
configurationParameter("my.parameter1", "true")
88+
configurationParameter("my.parameter2", "1234")
89+
instrumentationTests.useConfigurationParameters.set(false)
90+
}
91+
project.evaluate()
92+
93+
assertThat(project.android.defaultConfig.testInstrumentationRunnerArguments["configurationParameters"])
94+
.isNull()
95+
}
96+
6897
/* Dependencies */
6998

7099
@Test
71-
fun `add only the main dependencies`() {
72-
project.addJUnitJupiterApi()
100+
fun `add only the main dependencies (test)`() {
101+
project.addJUnitJupiterApi("test")
102+
project.evaluate()
103+
104+
assertThat(project).configuration("testImplementation").hasDependency(coreLibrary())
105+
assertThat(project).configuration("testRuntimeOnly").doesNotHaveDependency(runnerLibrary())
106+
107+
assertThat(project).configuration("testImplementation").doesNotHaveDependency(extensionsLibrary())
108+
assertThat(project).configuration("testImplementation").doesNotHaveDependency(composeLibrary())
109+
}
110+
111+
@Test
112+
fun `add only the main dependencies (androidTest)`() {
113+
project.addJUnitJupiterApi("androidTest")
73114
project.evaluate()
74115

75116
assertThat(project).configuration("androidTestImplementation").hasDependency(coreLibrary())
@@ -81,7 +122,7 @@ class InstrumentationSupportTests {
81122

82123
@Test
83124
fun `allow overriding the version`() {
84-
project.addJUnitJupiterApi()
125+
project.addJUnitJupiterApi("androidTest")
85126
project.junitPlatform.instrumentationTests.version.set("1.3.3.7")
86127
project.evaluate()
87128

@@ -91,7 +132,7 @@ class InstrumentationSupportTests {
91132

92133
@Test
93134
fun `allow overriding the dependencies`() {
94-
project.addJUnitJupiterApi()
135+
project.addJUnitJupiterApi("androidTest")
95136
val addedCore = "de.mannodermaus.junit5:android-test-core:0.1.3.3.7"
96137
val addedRunner = "de.mannodermaus.junit5:android-test-runner:0.1.3.3.7"
97138
project.dependencies.add("androidTestImplementation", addedCore)
@@ -104,7 +145,7 @@ class InstrumentationSupportTests {
104145

105146
@Test
106147
fun `do not add the dependencies when disabled`() {
107-
project.addJUnitJupiterApi()
148+
project.addJUnitJupiterApi("androidTest")
108149
project.junitPlatform.instrumentationTests.enabled.set(false)
109150
project.evaluate()
110151

@@ -132,7 +173,7 @@ class InstrumentationSupportTests {
132173

133174
@Test
134175
fun `add the extension library if configured`() {
135-
project.addJUnitJupiterApi()
176+
project.addJUnitJupiterApi("androidTest")
136177
project.junitPlatform.instrumentationTests.includeExtensions.set(true)
137178
project.evaluate()
138179

@@ -143,22 +184,31 @@ class InstrumentationSupportTests {
143184

144185
@Test
145186
fun `add the compose library if configured`() {
146-
project.addJUnitJupiterApi()
147-
project.addCompose()
187+
project.addJUnitJupiterApi("test")
188+
project.addJUnitJupiterApi("androidTest")
189+
project.addCompose("test")
190+
project.addCompose("androidTest")
148191
project.evaluate()
149192

193+
assertThat(project).configuration("testImplementation").hasDependency(coreLibrary())
194+
assertThat(project).configuration("testImplementation").hasDependency(composeLibrary())
150195
assertThat(project).configuration("androidTestImplementation").hasDependency(coreLibrary())
151196
assertThat(project).configuration("androidTestImplementation").hasDependency(composeLibrary())
152197
assertThat(project).configuration("androidTestRuntimeOnly").hasDependency(runnerLibrary())
153198
}
154199

155200
@Test
156201
fun `add the extensions and compose libraries if configured`() {
157-
project.addJUnitJupiterApi()
158-
project.addCompose()
202+
project.addJUnitJupiterApi("test")
203+
project.addJUnitJupiterApi("androidTest")
204+
project.addCompose("test")
205+
project.addCompose("androidTest")
159206
project.junitPlatform.instrumentationTests.includeExtensions.set(true)
160207
project.evaluate()
161208

209+
assertThat(project).configuration("testImplementation").hasDependency(coreLibrary())
210+
assertThat(project).configuration("testImplementation").hasDependency(composeLibrary())
211+
assertThat(project).configuration("testImplementation").hasDependency(extensionsLibrary())
162212
assertThat(project).configuration("androidTestImplementation").hasDependency(coreLibrary())
163213
assertThat(project).configuration("androidTestImplementation").hasDependency(composeLibrary())
164214
assertThat(project).configuration("androidTestImplementation").hasDependency(extensionsLibrary())
@@ -167,7 +217,7 @@ class InstrumentationSupportTests {
167217

168218
@Test
169219
fun `register the filter-write tasks`() {
170-
project.addJUnitJupiterApi()
220+
project.addJUnitJupiterApi("androidTest")
171221
project.evaluate()
172222

173223
// AGP only registers androidTest tasks for the debug build type
@@ -177,12 +227,14 @@ class InstrumentationSupportTests {
177227

178228
/* Private */
179229

180-
private fun Project.addJUnitJupiterApi() {
181-
dependencies.add("androidTestImplementation", "org.junit.jupiter:junit-jupiter-api:+")
230+
private fun Project.addJUnitJupiterApi(prefix: String) {
231+
val configuration = if (prefix.isEmpty()) "implementation" else "${prefix}Implementation"
232+
dependencies.add(configuration, "org.junit.jupiter:junit-jupiter-api:+")
182233
}
183234

184-
private fun Project.addCompose() {
185-
dependencies.add("androidTestImplementation", "androidx.compose.ui:ui-test-android:+")
235+
private fun Project.addCompose(prefix: String) {
236+
val configuration = if (prefix.isEmpty()) "implementation" else "${prefix}Implementation"
237+
dependencies.add(configuration, "androidx.compose.ui:ui-test-android:+")
186238
}
187239

188240
private fun composeLibrary(withVersion: String? = Libraries.instrumentationVersion) =

0 commit comments

Comments
 (0)