44
55A Gradle plugin that allows for the execution of [ JUnit 5] [ junit5gh ] tests in Android environments.
66
7+ ## Why a separate plugin?
8+
9+ The JUnit Platform team provides a Gradle plugin for running JUnit 5 on the JVM. However,
10+ this plugin is tailored to the needs of a "purely Java" application, and doesn't work in
11+ the context of the multi-variant world that we live in on Android. Therefore, this plugin was born.
12+
13+ It configures a ` junitPlatformTest ` task for each registered build variant of a project.
14+ Furthermore, it automatically attaches both the Jupiter & Vintage Engines
15+ during the execution phase of your tests as well, so there's very little configuration
16+ necessary to get your project up-and-running on the JUnit Platform.
17+
18+ Instructions on how to write JUnit 5 tests can be found [ in their User Guide] [ junit5ug ] .
19+ Furthermore, this repository provides a small showcase of the functionality provided by JUnit 5 [ here] [ sampletests ] .
20+
721## Download
822
923``` groovy
1024buildscript {
1125 dependencies {
12- classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.21 "
26+ classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.22 "
1327 }
1428}
1529```
@@ -23,32 +37,99 @@ apply plugin: "com.android.application"
2337apply plugin: "de.mannodermaus.android-junit5"
2438
2539dependencies {
26- testImplementation junit5()
40+ // (Required) Writing and executing Unit Tests on the JUnit Platform.
41+ testImplementation junit5.unitTests()
2742
28- // (Optional) If you need "parameterized tests"
29- testImplementation junit5Params ()
43+ // (Optional) If you need "Parameterized Tests".
44+ testImplementation junit5.parameterized ()
3045
31- // (Optional) For running tests inside Android Studio 3.x (see below for details)
32- testCompileOnly junit5EmbeddedRuntime()
46+ // (Optional) For running tests inside Android Studio 3.x
47+ // Please refer to the "Android Studio Workarounds" section for more insight on this.
48+ testCompileOnly junit5.unitTestsRuntime()
49+
50+ // (Optional) Writing and executing Instrumented Tests with the JUnit Platform Runner.
51+ //
52+ // IMPORTANT:
53+ // By declaring this dependency, you have to use a minSdkVersion
54+ // of at least 26, since the nature of JUnit 5 relies on APIs that aren't
55+ // available on Android devices before then.
56+ // Additionally, you are required to explicitly enable support for instrumented tests in the
57+ // "junitPlatform" configuration closure (see the section below for details).
58+ androidTestImplementation junit5.instrumentationTests()
3359}
3460```
3561
36- ## Usage
62+ ## Configuration
3763
38- This plugin configures a ` junitPlatformTest ` task for each registered build variant of a project.
39- It automatically attaches both the Jupiter & Vintage Engines during the execution phase of your tests as well.
64+ The plugin applies a configuration closure to your module's ` android.testOptions ` .
65+ Inside it, you can use [ all properties available through the default JUnit 5 Gradle plugin] [ junit5config ] .
66+ However, there are a few more parameters that allow for more customization of the JUnit Platform
67+ in your Android project. These are detailed below, alongside their default values:
4068
41- More instructions on how to write JUnit 5 tests can be found [ in their User Guide] [ junit5ug ] .
42- Furthermore, this repository provides a small showcase of the functionality provided by JUnit 5 [ here] [ sampletests ] .
69+ ``` groovy
70+ android {
71+ testOptions {
72+ // Configuration closure added by the plugin;
73+ // all configurable parameters related to JUnit 5 can be found here
74+ junitPlatform {
75+ // The JUnit Jupiter dependency version to use
76+ jupiterVersion "5.0.2"
77+
78+ // The JUnit Vintage Engine dependency version to use
79+ vintageVersion "4.12.2"
80+
81+ // Whether or not JUnit 5 test tasks should be affected by
82+ // JVM Arguments, System Properties & Environment Variables
83+ // declared through "unitTests.all" closures
84+ applyDefaultTestOptions true
85+
86+ // Options related to running instrumented tests with JUnit 5.
87+ // This is an incubating feature which utilizes the backwards-compatibility
88+ // of the JUnit Platform in order to enhance the default Test Instrumentation Runner
89+ // with new power. However, because of their experimental nature and steep minSdkVersion requirement,
90+ // they are turned off by default. If you choose to enable them, you also have to declare
91+ // the library dependency in your androidTest scope. Please refer to the "Setup"
92+ // section for more details.
93+ instrumentationTests {
94+ enabled false
95+
96+ // The Android-Instrumentation-Test dependency version to use
97+ version "0.1.0"
98+ }
99+
100+ // Configuration of companion tasks for JaCoCo Reports,
101+ // associated with each JUnit 5 task generated by the plugin.
102+ // Just like the companion tasks themselves, these properties
103+ // will only have an effect if your module declares the "jacoco" plugin as well.
104+ // For each of the available report types, you can toggle the availability
105+ // and destination folders that they write to.
106+ jacoco {
107+ xml {
108+ enabled true
109+ destination project.file()
110+ }
111+ html {
112+ enabled true
113+ destination project.file()
114+ }
115+ csv {
116+ enabled true
117+ destination project.file()
118+ }
119+ }
120+ }
121+ }
122+ }
123+ ```
43124
44125## Android Studio Workarounds
45126
46127> ** Note:**
47- >
128+ >
48129> The following section deals with fixing Test Execution within ** Android Studio 3** .
49130> Running your JUnit 5 tests directly from Android Studio 2.3.3 and earlier ** will not work** :
50131> You will encounter an ` AbstractMethodError ` when trying to do so ([ more information here] [ as2issue ] ).
51- >
132+ >
52133> The cause of this error is similar in nature to the one described below, and related to outdated APIs.
53134> Unlike that issue though, we can't fix the ` AbstractMethodError ` inside IntelliJ's internal runtime
54135> in the same way. Therefore, please resort to using Gradle for unit testing in Android Studio 2.
@@ -73,52 +154,13 @@ To use this, add the following line alongside the other `junit5()` dependencies:
73154
74155``` groovy
75156dependencies {
76- testCompileOnly junit5EmbeddedRuntime()
77- }
78- ```
79-
80- ## Extras
81-
82- ### Override Dependency Versions
83-
84- Inside the configuration closure applied by the plugin, you can specify the same properties as you would
85- for a Java-based project with the JUnit Platform Gradle plugin.
86- However, there are some additional properties that you can apply:
87-
88- ``` groovy
89- junitPlatform {
90- // The JUnit Jupiter dependency version to use; matches the platform's version by default
91- jupiterVersion "5.0.2"
92- // The JUnit Vintage Engine dependency version to use; matches the platform's version by default
93- vintageVersion "4.12.2"
94- }
95- ```
96-
97- ### JaCoCo Integration
98-
99- If the plugin detects the usage of [ JaCoCo] [ jacoco ] inside a project that it's being applied to,
100- it will automatically configure additional tasks to report the unit test coverage
101- of your application based on its JUnit 5 tests.
102- There is no additional setup required to enable this behaviour.
103- You can however customize the reports JaCoCo should generate.
104-
105- Configuration is applied through the ` jacoco ` clause inside the plugin's DSL:
106-
107- ``` groovy
108- apply plugin: "jacoco"
109-
110- junitPlatform {
111- jacoco {
112- csvReport true
113- xmlReport true
114- htmlReport true
115- }
157+ testCompileOnly junit5.unitTestsRuntime()
116158}
117159```
118160
119161## Licenses
120162
121- #### ` android-junit5-embedded-runtime `
163+ #### android-junit5-embedded-runtime:
122164
123165```
124166Copyright 2000-2016 JetBrains s.r.o.
@@ -138,7 +180,7 @@ limitations under the License.
138180
139181See also the [ full License text] ( android-junit5-embedded-runtime/LICENSE ) .
140182
141- #### Others :
183+ #### Everything else :
142184
143185```
144186Copyright 2017 Marcel Schnelle
@@ -161,7 +203,8 @@ See also the [full License text](LICENSE).
161203
162204
163205 [ junit5gh ] : https://github.com/junit-team/junit5
164- [ junit5ug ] : http://junit.org/junit5/docs/current/user-guide
206+ [ junit5ug ] : https://junit.org/junit5/docs/current/user-guide
207+ [ junit5config ] : http://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle-junit-configure
165208 [ travisci ] : https://travis-ci.org/mannodermaus/android-junit5
166209 [ as2issue ] : https://github.com/mannodermaus/android-junit5/issues/19
167210 [ jacoco ] : http://www.eclemma.org/jacoco
0 commit comments