-
Notifications
You must be signed in to change notification settings - Fork 53
/
README.md.template
316 lines (231 loc) · 10.5 KB
/
README.md.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# <img src=".images/logo.png" align="right" width="100">android-junit5 [![CircleCI](https://circleci.com/gh/mannodermaus/android-junit5/tree/main.svg?style=svg)][circleci]
A Gradle plugin that allows for the execution of [JUnit 5][junit5gh] tests in Android environments using **Android Gradle Plugin ${constants.minimumRequiredAgpVersion} or later.**
## How?
This plugin configures the unit test tasks for each build variant of a project to run on the JUnit Platform. Furthermore, it provides additional configuration options for these tests [through a DSL][wiki-dsl] and facilitates the usage of JUnit 5 for instrumentation tests.
Instructions on how to write tests with the JUnit 5 framework can be found [in their User Guide][junit5ug]. To get a first look at its features, a small showcase project can be found [here][sampletests].
## Setup
To get started, declare the plugin in your `app` module's build script alongside the latest version. Snapshots of the development version are available through [Sonatype's `snapshots` repository][sonatyperepo].
<details open>
<summary>Kotlin</summary>
```kotlin
plugins {
id("de.mannodermaus.android-junit5") version "${pluginVersion}"
}
dependencies {
// (Required) Writing and executing Unit Tests on the JUnit Platform
testImplementation("${libs.junitJupiterApi}")
testRuntimeOnly("${libs.junitJupiterEngine}")
// (Optional) If you need "Parameterized Tests"
testImplementation("${libs.junitJupiterParams}")
// (Optional) If you also have JUnit 4-based tests
testImplementation("${libs.junit4}")
testRuntimeOnly("${libs.junitVintageEngine}")
}
```
</details>
<details>
<summary>Groovy</summary>
```groovy
plugins {
id "de.mannodermaus.android-junit5" version "${pluginVersion}"
}
dependencies {
// (Required) Writing and executing Unit Tests on the JUnit Platform
testImplementation "${libs.junitJupiterApi}"
testRuntimeOnly "${libs.junitJupiterEngine}"
// (Optional) If you need "Parameterized Tests"
testImplementation "${libs.junitJupiterParams}"
// (Optional) If you also have JUnit 4-based tests
testImplementation "${libs.junit4}"
testRuntimeOnly "${libs.junitVintageEngine}"
}
```
</details>
<br/>
### Alternative: Legacy DSL
If you prefer to use the legacy way to declare the dependency instead, remove the `version()` block from above and declare the plugin in your _root project's build script_ like so:
<details>
<summary>Kotlin</summary>
```kotlin
// In the root project's build.gradle.kts:
buildscript {
dependencies {
classpath("de.mannodermaus.gradle.plugins:android-junit5:${pluginVersion}")
}
}
// In the app module's build.gradle.kts:
plugins {
id("de.mannodermaus.android-junit5")
}
```
</details>
<details>
<summary>Groovy</summary>
```groovy
// In the root project's build.gradle:
buildscript {
dependencies {
classpath "de.mannodermaus.gradle.plugins:android-junit5:${pluginVersion}"
}
}
// In the app module's build.gradle:
apply plugin: "de.mannodermaus.android-junit5"
```
</details>
<br/>
More information on Getting Started can be found [on the wiki][wiki-gettingstarted].
## Requirements
The latest version of this plugin requires:
* Android Gradle Plugin `${constants.minimumRequiredAgpVersion}` or above
* Gradle `${constants.minimumRequiredGradleVersion}` or above
## Instrumentation Test Support
You can use JUnit 5 to run instrumentation tests on emulators and physical devices, too. Because the framework is built on Java 8 from the ground up, these instrumentation tests will only run on devices running Android 8.0 (API 26) or newer – older phones will skip the execution of these tests completely, marking them as "ignored".
Before you can write instrumentation tests with JUnit Jupiter, make sure that your module is using the `androidx.test.runner.AndroidJUnitRunner` (or a subclass of it) as its `testInstrumentationRunner`. Then, simply add a dependency on `junit-jupiter-api` to the `androidTestImplementation` configuration in your build script and the plugin will automatically configure JUnit 5 tests for you:
<details open>
<summary>Kotlin</summary>
```kotlin
dependencies {
androidTestImplementation("${libs.junitJupiterApi}")
}
```
</details>
<details>
<summary>Groovy</summary>
```groovy
dependencies {
androidTestImplementation "${libs.junitJupiterApi}"
}
```
</details>
By enabling JUnit 5 for instrumentation tests, you will gain access to `ActivityScenarioExtension` (among other things), which helps with the orchestration of `Activity` classes. Check [the wiki][wiki-home] for more info.
### Extensions
An optional artifact with more helper extensions is available for specific use cases.
It contains the following APIs:
- `GrantPermissionExtension` for granting permissions before each test
Can you think of more? Let's discuss in the issues section!
<details open>
<summary>Kotlin</summary>
```kotlin
junitPlatform {
instrumentationTests.includeExtensions.set(true)
}
```
</details>
<details>
<summary>Groovy</summary>
```groovy
junitPlatform {
instrumentationTests.includeExtensions.set(true)
}
```
</details>
### Jetpack Compose
To test `@Composable` functions on device with JUnit 5, first enable support for instrumentation tests as described above.
Then, add the Compose test dependency to your `androidTestImplementation` configuration
and the plugin will autoconfigure JUnit 5 Compose support for you!
<details open>
<summary>Kotlin</summary>
```kotlin
dependencies {
// Compose test framework
androidTestImplementation("androidx.compose.ui:ui-test-android:$compose_version")
// Needed for createComposeExtension() and createAndroidComposeExtension()
debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")
}
```
</details>
<details>
<summary>Groovy</summary>
```groovy
dependencies {
// Compose test framework
androidTestImplementation "androidx.compose.ui:ui-test-android:$compose_version"
// Needed for createComposeExtension() and createAndroidComposeExtension()
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
}
```
</details>
[The wiki][wiki-home] includes a section on how to test your Composables with JUnit 5.
### Override the version of instrumentation test libraries
By default, the plugin will make sure to use a compatible version of the instrumentation test libraries
when it sets up the artifacts automatically. However, it is possible to choose a custom version instead via its DSL:
<details open>
<summary>Kotlin</summary>
```kotlin
junitPlatform {
instrumentationTests.version.set("${instrumentationVersion}")
}
```
</details>
<details>
<summary>Groovy</summary>
```groovy
junitPlatform {
instrumentationTests.version.set("${instrumentationVersion}")
}
```
</details>
## Official Support
At this time, Google hasn't shared any immediate plans to bring first-party support for JUnit 5 to Android. The following list is an aggregation of pending feature requests:
- [InstantTaskExecutorRule uses @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) -- why? (issuetracker.google.com)](https://issuetracker.google.com/u/0/issues/79189568)
- [Add support for JUnit 5 (issuetracker.google.com)](https://issuetracker.google.com/issues/127100532)
- [JUnit 5 support (github.com/android/android-test)](https://github.com/android/android-test/issues/224)
## Support for @Rules
Since JUnit 5 has replaced the `@Rule` mechanism with Extensions, the following artifacts help bridge the gap until Android officially transitions to JUnit 5.
### InstantExecutorExtension
Replaces `InstantTaskExecutorRule` in JUnit 5.
<details>
<summary>Kotlin</summary>
```kotlin
dependencies {
testImplementation("${libs.instantTaskExecutorExtension}")
}
```
</details>
<details>
<summary>Groovy</summary>
```groovy
dependencies {
testImplementation '${libs.instantTaskExecutorExtension}'
}
```
</details>
For more details see [instant-task-executor-extension](https://github.com/neboskreb/instant-task-executor-extension) on GitHub.
## Building Locally
This repository contains multiple modules, divided into two sub-projects. The repository's root directory contains build logic shared across the sub-projects, which in turn use symlinks to connect to the common build scripts in their parent folder.
- `instrumentation`: The root folder for Android-based modules, namely the instrumentation libraries & a sample application. After cloning, open this project in Android Studio.
- `plugin`: The root folder for Java-based modules, namely the Gradle plugin for JUnit 5 on Android, as well as its test module. After cloning, open this project in IntelliJ IDEA.
## Plugin Compatibility Map
For users that cannot match the current minimum version requirement of the Android Gradle Plugin requested by this plugin,
refer to the table below to find a suitable alternative version. Note that **no active development will go into these
legacy versions**, so please consider upgrading to at least Android Gradle Plugin ${constants.minimumRequiredAgpVersion}
before filing an issue with the latest one.
|Your AGP Version|Suggested JUnit5 Plugin Version|
|---|---|
|`>= 8.0.0`|`${pluginVersion}`|
|`7.0.0` - `7.4.2`|`1.10.0.0`|
|`4.0.0` - `4.2.2`|`1.8.2.1`|
|`3.5.0` - `3.6.4`|`1.7.1.1`|
|`< 3.5.0`|none; you should **really** update your build env, bro|
## License
```
Copyright 2017-${constants.currentYear} Marcel Schnelle
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```
See also the [full License text](LICENSE).
[junit5gh]: https://github.com/junit-team/junit5
[junit5ug]: https://junit.org/junit5/docs/current/user-guide
[circleci]: https://circleci.com/gh/mannodermaus/android-junit5
[sonatyperepo]: https://oss.sonatype.org/content/repositories/snapshots
[sampletests]: instrumentation/sample
[wiki-home]: https://github.com/mannodermaus/android-junit5/wiki
[wiki-dsl]: https://github.com/mannodermaus/android-junit5/wiki/Configuration
[wiki-gettingstarted]: https://github.com/mannodermaus/android-junit5/wiki/Getting-Started