|
1 package com.geekorum.build |
|
2 |
|
3 import com.android.build.gradle.BaseExtension |
|
4 import com.android.build.gradle.internal.dsl.TestOptions |
|
5 import org.gradle.api.Project |
|
6 import org.gradle.api.artifacts.Dependency |
|
7 import org.gradle.api.artifacts.DependencyConstraint |
|
8 import org.gradle.api.artifacts.ExternalModuleDependency |
|
9 import org.gradle.api.artifacts.dsl.DependencyConstraintHandler |
|
10 import org.gradle.api.artifacts.dsl.DependencyHandler |
|
11 import org.gradle.kotlin.dsl.closureOf |
|
12 import org.gradle.kotlin.dsl.configure |
|
13 import org.gradle.kotlin.dsl.dependencies |
|
14 import org.gradle.kotlin.dsl.kotlin |
|
15 |
|
16 const val espressoVersion = "3.2.0" |
|
17 const val androidxTestRunnerVersion = "1.3.0-alpha05" |
|
18 const val androidxTestCoreVersion = "1.3.0-alpha05" |
|
19 const val robolectricVersion = "4.3.1" |
|
20 |
|
21 |
|
22 /* |
|
23 * Configuration for espresso and robolectric usage in an Android project |
|
24 */ |
|
25 internal fun Project.configureTests() { |
|
26 extensions.configure<BaseExtension> { |
|
27 defaultConfig { |
|
28 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" |
|
29 testInstrumentationRunnerArgument("clearPackageData", "true") |
|
30 testInstrumentationRunnerArgument("disableAnalytics", "true") |
|
31 } |
|
32 |
|
33 testOptions { |
|
34 execution = "ANDROIDX_TEST_ORCHESTRATOR" |
|
35 animationsDisabled = true |
|
36 |
|
37 unitTests(closureOf<TestOptions.UnitTestOptions> { |
|
38 isIncludeAndroidResources = true |
|
39 }) |
|
40 } |
|
41 } |
|
42 |
|
43 |
|
44 dependencies { |
|
45 dualTestImplementation(kotlin("test-junit")) |
|
46 |
|
47 androidTestUtil("androidx.test:orchestrator:$androidxTestRunnerVersion") |
|
48 androidTestImplementation("androidx.test:runner:$androidxTestRunnerVersion") |
|
49 dualTestImplementation("androidx.test.ext:junit-ktx:1.1.1") |
|
50 |
|
51 dualTestImplementation("androidx.test:core-ktx:$androidxTestCoreVersion") |
|
52 dualTestImplementation("androidx.test:rules:$androidxTestRunnerVersion") |
|
53 // fragment testing is usually declared on debugImplementation configuration and need these dependencies |
|
54 constraints { |
|
55 debugImplementation("androidx.test:core:$androidxTestCoreVersion") |
|
56 debugImplementation("androidx.test:monitor:$androidxTestRunnerVersion") |
|
57 } |
|
58 |
|
59 dualTestImplementation("androidx.test.espresso:espresso-core:$espressoVersion") |
|
60 dualTestImplementation("androidx.test.espresso:espresso-contrib:$espressoVersion") |
|
61 dualTestImplementation("androidx.test.espresso:espresso-intents:$espressoVersion") |
|
62 |
|
63 // assertions |
|
64 dualTestImplementation("com.google.truth:truth:1.0") |
|
65 dualTestImplementation("androidx.test.ext:truth:1.3.0-alpha01") |
|
66 |
|
67 // mock |
|
68 testImplementation("io.mockk:mockk:1.9.3") |
|
69 androidTestImplementation("io.mockk:mockk-android:1.9.3") |
|
70 testImplementation("org.robolectric:robolectric:$robolectricVersion") |
|
71 |
|
72 constraints { |
|
73 dualTestImplementation(kotlin("reflect")) { |
|
74 because("Use the kotlin version that we use") |
|
75 } |
|
76 androidTestImplementation("org.objenesis:objenesis") { |
|
77 because("3.x version use instructions only available with minSdk 26 (Android O)") |
|
78 version { |
|
79 strictly("2.6") |
|
80 } |
|
81 } |
|
82 } |
|
83 } |
|
84 } |
|
85 |
|
86 fun DependencyHandler.dualTestImplementation(dependencyNotation: Any) { |
|
87 add("androidTestImplementation", dependencyNotation) |
|
88 add("testImplementation", dependencyNotation) |
|
89 } |
|
90 |
|
91 fun DependencyHandler.dualTestImplementation(dependencyNotation: Any, action: ExternalModuleDependency.() -> Unit) { |
|
92 val closure = closureOf(action) |
|
93 add("androidTestImplementation", dependencyNotation, closure) |
|
94 add("testImplementation", dependencyNotation, closure) |
|
95 } |
|
96 |
|
97 internal fun DependencyHandler.androidTestImplementation(dependencyNotation: Any): Dependency? = |
|
98 add("androidTestImplementation", dependencyNotation) |
|
99 |
|
100 internal fun DependencyHandler.androidTestImplementation(dependencyNotation: Any, action: ExternalModuleDependency.() -> Unit) { |
|
101 val closure = closureOf(action) |
|
102 add("androidTestImplementation", dependencyNotation, closure) |
|
103 } |
|
104 |
|
105 internal fun DependencyHandler.androidTestUtil(dependencyNotation: Any): Dependency? = |
|
106 add("androidTestUtil", dependencyNotation) |
|
107 |
|
108 internal fun DependencyHandler.testImplementation(dependencyNotation: Any): Dependency? = |
|
109 add("testImplementation", dependencyNotation) |
|
110 |
|
111 internal fun DependencyConstraintHandler.debugImplementation(dependencyConstraintNotation: Any): DependencyConstraint? = |
|
112 add("debugImplementation", dependencyConstraintNotation) |