You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Gradle plugin that allows for the execution of [JUnit 5][junit5gh] tests in Android environments using **Android Gradle Plugin 8.2 or later.**
8
+
A Gradle plugin that allows for the execution of [JUnit Framework][junit-framework-github] tests in Android environments using **Android Gradle Plugin 8.2 or later.**
9
9
10
10
## How?
11
11
12
-
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.
12
+
This plugin configures the unit test tasks for each build variant of a project to run on the JUnit Platform.
13
+
Furthermore, it provides additional configuration options for these tests [through a DSL][wiki-dsl]
14
+
and facilitates the usage of JUnit Framework for instrumentation tests.
13
15
14
-
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].
16
+
Instructions on how to write tests with the JUnit Framework can be found [in their User Guide][junit-framework-guide].
17
+
To get a first look at its features, a small showcase project can be found [here][sampletests].
15
18
16
19
## Setup
17
20
18
-
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].
21
+
To get started, declare the plugin in your `app` module's build script alongside the latest version.
22
+
Snapshots of the development version are available through [Sonatype's `snapshots` repository][sonatyperepo].
19
23
20
24
<detailsopen>
21
25
<summary>Kotlin</summary>
22
26
23
27
```kotlin
24
28
plugins {
29
+
// 1. Apply the plugin
25
30
id("de.mannodermaus.android-junit5") version "1.14.0.0"
26
31
}
27
32
28
33
dependencies {
29
-
// (Required) Writing and executing Unit Tests on the JUnit Platform
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:
More information on Getting Started can be found [on the wiki][wiki-gettingstarted].
109
73
110
74
## Requirements
111
75
112
-
The latest version of this plugin requires:
113
-
* Android Gradle Plugin `8.2` or above
114
-
* Gradle `8.2` or above
76
+
The latest version of this plugin requires at least:
77
+
* Android Gradle Plugin `8.2`
78
+
* Gradle `8.2`
115
79
116
80
## Instrumentation Test Support
117
81
118
-
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".
82
+
You can also write instrumentation tests with new JUnit APIs and execute them on emulators and physical devices.
83
+
Depending on the Java requirements of the JUnit Framework version, these instrumentation tests will only run on devices
84
+
that meet these requirements, however. These tests are ignored and their execution will be skipped on older devices.
119
85
120
-
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:
86
+
- JUnit 5 requires Java 8 and is only supported by devices running Android 8.0 (API 26) or newer
87
+
- JUnit 6 requires Java 17 and is only supported by devices running Android 15 (API 35) or newer
88
+
89
+
Before you can write instrumentation tests with JUnit Jupiter,
90
+
make sure that your module is using the `androidx.test.runner.AndroidJUnitRunner`
91
+
(or a subclass of it) as its `testInstrumentationRunner`. Then, simply add a dependency on JUnit Jupiter API
92
+
to the `androidTestImplementation` configuration in your build script and the plugin will
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.
117
+
By enabling JUnit Framework for instrumentation tests, you will gain access to `ActivityScenarioExtension` among other things,
118
+
which helps with the orchestration of `Activity` classes. Check [the wiki][wiki-home] for more info.
143
119
144
120
### Extensions
145
121
146
-
An optional artifact with more helper extensions is available for specific use cases.
147
-
It contains the following APIs:
122
+
An optional artifact with `extensions` is available for specific use cases. It contains the following APIs:
148
123
149
124
-`GrantPermissionExtension` for granting permissions before each test
150
125
@@ -172,15 +147,17 @@ Can you think of more? Let's discuss in the issues section!
172
147
173
148
### Jetpack Compose
174
149
175
-
To test `@Composable` functions on device with JUnit 5, first enable support for instrumentation tests as described above.
176
-
Then, add the Compose test dependency to your `androidTestImplementation` configuration
177
-
and the plugin will autoconfigure JUnit 5 Compose support for you!
150
+
To test `@Composable` functions on devices compatible with the JUnit Framework,
151
+
enable support for instrumentation tests as described above. Then add the Compose test dependency
152
+
to your `androidTestImplementation` configuration and the plugin will autoconfigure JUnit 5 Compose support for you.
178
153
179
154
<detailsopen>
180
155
<summary>Kotlin</summary>
181
156
182
157
```kotlin
183
158
dependencies {
159
+
// Setup from the previous section for enabling instrumentation tests...
@@ -204,7 +183,7 @@ and the plugin will autoconfigure JUnit 5 Compose support for you!
204
183
```
205
184
</details>
206
185
207
-
[The wiki][wiki-home] includes a section on how to test your Composables with JUnit 5.
186
+
[The wiki][wiki-home] includes a section on how to test your Composables.
208
187
209
188
### Override the version of instrumentation test libraries
210
189
@@ -233,15 +212,17 @@ when it sets up the artifacts automatically. However, it is possible to choose a
233
212
234
213
## Official Support
235
214
236
-
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:
215
+
At this time, Google hasn't shared any immediate plans to bring first-party support for anything beyond JUnit 4 to Android.
216
+
The following list is an aggregation of pending feature requests:
-[Add support for JUnit 5 (issuetracker.google.com)](https://issuetracker.google.com/issues/127100532)
240
220
-[JUnit 5 support (github.com/android/android-test)](https://github.com/android/android-test/issues/224)
241
221
242
222
## Support for @Rules
243
223
244
-
Since JUnit 5 has replaced the `@Rule` mechanism with Extensions, the following artifacts help bridge the gap until Android officially transitions to JUnit 5.
224
+
Since the JUnit Framework has replaced the `@Rule` mechanism with the concept of an `Extension`,
225
+
the following artifacts help bridge the gap until Android officially transitions, if ever.
245
226
246
227
### InstantExecutorExtension
247
228
@@ -252,7 +233,7 @@ Replaces `InstantTaskExecutorRule` in JUnit 5.
@@ -271,19 +252,20 @@ For more details see [instant-task-executor-extension](https://github.com/nebosk
271
252
272
253
## Building Locally
273
254
274
-
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.
255
+
This repository contains multiple modules, divided into two sub-projects.
256
+
The repository's root directory contains build logic shared across the sub-projects,
257
+
which in turn use symlinks to connect to the common build scripts in their parent folder.
275
258
276
-
-`instrumentation`: The root folder for Android-based modules, namely the instrumentation libraries & a sample application. After cloning, open this project in Android Studio.
277
-
-`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.
259
+
-`instrumentation`: The root folder for the instrumentation libraries & a sample. Open this folder in Android Studio.
260
+
-`plugin`: The root folder for the Gradle plugin. Open this folder in IntelliJ IDEA.
278
261
279
262
## Plugin Compatibility Map
280
263
281
264
For users that cannot match the current minimum version requirement of the Android Gradle Plugin requested by this plugin,
282
-
refer to the table below to find a suitable alternative version. Note that **no active development will go into these
283
-
legacy versions**, so please consider upgrading to at least Android Gradle Plugin 8.2
284
-
before filing an issue with the latest one.
265
+
refer to the table below to find a suitable alternative version. Note that **no active development will go into legacy versions**,
266
+
so please consider upgrading to at least AGP 8.2 before filing an issue with the latest one.
0 commit comments