|
| 1 | +# android-junit5 [][circleci] |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +A Gradle plugin that allows for the execution of [JUnit 5][junit5gh] tests in Android environments using **Android Gradle Plugin 3.2.0 or later.** |
| 6 | + |
| 7 | +## How? |
| 8 | + |
| 9 | +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] attached to `android.testOptions`. |
| 10 | + |
| 11 | +Instructions on how to write JUnit 5 tests can be found [in their User Guide][junit5ug]. |
| 12 | +Furthermore, this repository provides a small showcase of the functionality provided by JUnit 5 [here][sampletests]. |
| 13 | + |
| 14 | +## Download |
| 15 | + |
| 16 | +<details open> |
| 17 | + <summary>Groovy</summary> |
| 18 | + |
| 19 | + ```groovy |
| 20 | + buildscript { |
| 21 | + dependencies { |
| 22 | + classpath "de.mannodermaus.gradle.plugins:android-junit5:${pluginVersion}" |
| 23 | + } |
| 24 | + } |
| 25 | + ``` |
| 26 | +</details> |
| 27 | + |
| 28 | +<details> |
| 29 | + <summary>Kotlin</summary> |
| 30 | + |
| 31 | + ```kotlin |
| 32 | + buildscript { |
| 33 | + dependencies { |
| 34 | + classpath("de.mannodermaus.gradle.plugins:android-junit5:${pluginVersion}") |
| 35 | + } |
| 36 | + } |
| 37 | + ``` |
| 38 | +</details> |
| 39 | + |
| 40 | +<br/> |
| 41 | + |
| 42 | +Snapshots of the development version are available through [Sonatype's `snapshots` repository][sonatyperepo]. |
| 43 | + |
| 44 | +## Setup |
| 45 | + |
| 46 | +<details open> |
| 47 | + <summary>Groovy</summary> |
| 48 | + |
| 49 | + ```groovy |
| 50 | + apply plugin: "de.mannodermaus.android-junit5" |
| 51 | + |
| 52 | + dependencies { |
| 53 | + // (Required) Writing and executing Unit Tests on the JUnit Platform |
| 54 | + testImplementation "org.junit.jupiter:junit-jupiter-api:${Versions.org_junit_jupiter}" |
| 55 | + testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${Versions.org_junit_jupiter}" |
| 56 | + |
| 57 | + // (Optional) If you need "Parameterized Tests" |
| 58 | + testImplementation "org.junit.jupiter:junit-jupiter-params:${Versions.org_junit_jupiter}" |
| 59 | + |
| 60 | + // (Optional) If you also have JUnit 4-based tests |
| 61 | + testImplementation "junit:junit:${Versions.junit}" |
| 62 | + testRuntimeOnly "org.junit.vintage:junit-vintage-engine:${Versions.junit_vintage_engine}" |
| 63 | + } |
| 64 | + ``` |
| 65 | +</details> |
| 66 | + |
| 67 | +<details> |
| 68 | + <summary>Kotlin</summary> |
| 69 | + |
| 70 | + ```kotlin |
| 71 | + plugins { |
| 72 | + id("de.mannodermaus.android-junit5") |
| 73 | + } |
| 74 | + |
| 75 | + dependencies { |
| 76 | + // (Required) Writing and executing Unit Tests on the JUnit Platform |
| 77 | + testImplementation("org.junit.jupiter:junit-jupiter-api:${Versions.org_junit_jupiter}") |
| 78 | + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${Versions.org_junit_jupiter}") |
| 79 | + |
| 80 | + // (Optional) If you need "Parameterized Tests" |
| 81 | + testImplementation("org.junit.jupiter:junit-jupiter-params:${Versions.org_junit_jupiter}") |
| 82 | + |
| 83 | + // (Optional) If you also have JUnit 4-based tests |
| 84 | + testImplementation("junit:junit:${Versions.junit}") |
| 85 | + testRuntimeOnly("org.junit.vintage:junit-vintage-engine:${Versions.junit_vintage_engine}") |
| 86 | + } |
| 87 | + ``` |
| 88 | +</details> |
| 89 | + |
| 90 | +<br/> |
| 91 | + |
| 92 | +More information on Getting Started can be found [on the wiki][wiki-gettingstarted]. |
| 93 | + |
| 94 | +## Requirements |
| 95 | + |
| 96 | +The latest version of this plugin requires: |
| 97 | +* Android Gradle Plugin `3.2.0` or above |
| 98 | +* Gradle `4.7` or above |
| 99 | + |
| 100 | +## Instrumentation Test Support |
| 101 | + |
| 102 | +There is experimental support for Android instrumentation tests, which requires some additional configuration & dependencies. Furthermore, because JUnit 5 is built on Java 8 from the ground up, its 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". |
| 103 | + |
| 104 | +To start writing instrumentation tests with JUnit Jupiter, make the following changes to your module's build script: |
| 105 | + |
| 106 | +<details open> |
| 107 | + <summary>Groovy</summary> |
| 108 | + |
| 109 | + ```groovy |
| 110 | + android { |
| 111 | + defaultConfig { |
| 112 | + // 1) Make sure to use the AndroidJUnitRunner, of a subclass of it. This requires a dependency on androidx.test:runner, too! |
| 113 | + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" |
| 114 | + // 2) Connect JUnit 5 to the runner |
| 115 | + testInstrumentationRunnerArgument "runnerBuilder", "de.mannodermaus.junit5.AndroidJUnit5Builder" |
| 116 | + } |
| 117 | + |
| 118 | + // 3) Java 8 is required |
| 119 | + compileOptions { |
| 120 | + sourceCompatibility JavaVersion.VERSION_1_8 |
| 121 | + targetCompatibility JavaVersion.VERSION_1_8 |
| 122 | + } |
| 123 | + |
| 124 | + // 4) JUnit 5 will bundle in files with identical paths; exclude them |
| 125 | + packagingOptions { |
| 126 | + exclude "META-INF/LICENSE*" |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + dependencies { |
| 131 | + // 5) Jupiter API & Test Runner, if you don't have it already |
| 132 | + androidTestImplementation "androidx.test:runner:${Versions.androidx_test_runner}" |
| 133 | + androidTestImplementation "org.junit.jupiter:junit-jupiter-api:${Versions.org_junit_jupiter}" |
| 134 | + |
| 135 | + // 6) The instrumentation test companion libraries |
| 136 | + androidTestImplementation "de.mannodermaus.junit5:android-test-core:${instrumentationVersion}" |
| 137 | + androidTestRuntimeOnly "de.mannodermaus.junit5:android-test-runner:${instrumentationVersion}" |
| 138 | + } |
| 139 | + ``` |
| 140 | +</details> |
| 141 | + |
| 142 | +<details> |
| 143 | + <summary>Kotlin</summary> |
| 144 | + |
| 145 | + ```groovy |
| 146 | + android { |
| 147 | + defaultConfig { |
| 148 | + // 1) Make sure to use the AndroidJUnitRunner, of a subclass of it. This requires a dependency on androidx.test:runner, too! |
| 149 | + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" |
| 150 | + // 2) Connect JUnit 5 to the runner |
| 151 | + testInstrumentationRunnerArgument("runnerBuilder", "de.mannodermaus.junit5.AndroidJUnit5Builder") |
| 152 | + } |
| 153 | + |
| 154 | + // 3) Java 8 is required |
| 155 | + compileOptions { |
| 156 | + setSourceCompatibility(JavaVersion.VERSION_1_8) |
| 157 | + setTargetCompatibility(JavaVersion.VERSION_1_8) |
| 158 | + } |
| 159 | + |
| 160 | + // 4) JUnit 5 will bundle in files with identical paths; exclude them |
| 161 | + packagingOptions { |
| 162 | + exclude("META-INF/LICENSE*") |
| 163 | + } |
| 164 | + } |
| 165 | + dependencies { |
| 166 | + // 5) Jupiter API & Test Runner, if you don't have it already |
| 167 | + androidTestImplementation("androidx.test:runner:${Versions.androidx_test_runner}") |
| 168 | + androidTestImplementation("org.junit.jupiter:junit-jupiter-api:${Versions.org_junit_jupiter}") |
| 169 | + |
| 170 | + // 6) The instrumentation test companion libraries |
| 171 | + androidTestImplementation("de.mannodermaus.junit5:android-test-core:${instrumentationVersion}") |
| 172 | + androidTestRuntimeOnly("de.mannodermaus.junit5:android-test-runner:${instrumentationVersion}") |
| 173 | + } |
| 174 | + ``` |
| 175 | +</details> |
| 176 | + |
| 177 | +The `android-test-core` artifact includes an extension point for the `ActivityScenario` API; more information on that can be found in the wiki. |
| 178 | + |
| 179 | +# Official Support |
| 180 | + |
| 181 | +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: |
| 182 | + |
| 183 | +- [InstantTaskExecutorRule uses @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) -- why? (issuetracker.google.com)](https://issuetracker.google.com/u/0/issues/79189568) |
| 184 | +- [Add support for JUnit 5 (issuetracker.google.com)](https://issuetracker.google.com/issues/127100532) |
| 185 | +- [JUnit 5 support (github.com/android/android-test)](https://github.com/android/android-test/issues/224) |
| 186 | + |
| 187 | +# Building Locally |
| 188 | + |
| 189 | +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. |
| 190 | + |
| 191 | +- `instrumentation`: The root folder for Android-based modules, namely the instrumentation libraries & a sample application. After cloning, open this project in Android Studio. |
| 192 | +- `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. |
| 193 | + |
| 194 | +## License |
| 195 | + |
| 196 | +``` |
| 197 | +Copyright 2017-2019 Marcel Schnelle |
| 198 | + |
| 199 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 200 | +you may not use this file except in compliance with the License. |
| 201 | +You may obtain a copy of the License at |
| 202 | + |
| 203 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 204 | + |
| 205 | +Unless required by applicable law or agreed to in writing, software |
| 206 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 207 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 208 | +See the License for the specific language governing permissions and |
| 209 | +limitations under the License. |
| 210 | +``` |
| 211 | + |
| 212 | +See also the [full License text](LICENSE). |
| 213 | + |
| 214 | + [junit5gh]: https://github.com/junit-team/junit5 |
| 215 | + [junit5ug]: https://junit.org/junit5/docs/current/user-guide |
| 216 | + [circleci]: https://circleci.com/gh/mannodermaus/android-junit5 |
| 217 | + [sonatyperepo]: https://oss.sonatype.org/content/repositories/snapshots |
| 218 | + [sampletests]: instrumentation/sample |
| 219 | + [wiki-dsl]: https://github.com/mannodermaus/android-junit5/wiki/Configuration-DSL |
| 220 | + [wiki-gettingstarted]: https://github.com/mannodermaus/android-junit5/wiki/Getting-Started |
0 commit comments