I often find myself duplicating the exact same set of test classes such as mocks or helpers for both Android tests /androidTest
and unit tests /test
when writing tests for an application module.
For example, I have some static functions that help me set up mocks quickly in /test/MockUtils.java
However, I cannot reuse this helper class in any of my Android tests because they do not share the same class path - /androidTest
vs /test
.
I've thought of creating a new module that only contains test resources. However, this idea won't fly because the Android Gradle plugin refuses to depend on an app module.
project testCommon resolves to an APK archive which is not supported as a compilation dependency.
Is there any other way to create test classes that could be reused in both Android tests and unit tests?
First, let's start with the basics: JUnit is the default test framework for Android. Mockito, Robolectric, and Espresso all use it as the backbone and build on top of it. This is one decision you don't have to make. More specifically, AndroidJUnit4 is the current runner that provides utilities to run Android tests.
Create new tests You can add a new test for a specific class or method directly from its source code by following these steps: Open the source file that contains the code you want to test. Put your cursor in the name of the class or method you want to test, and press Control+Shift+T ( Command+Shift+T on macOS).
Create a local unit test class To do so, create a class that contains one or more test methods, usually in module-name/src/test/ . A test method begins with the @Test annotation and contains the code to exercise and verify a single aspect of the component that you want to test.
NOTE: This is a theorized solution, one that I have not tried.
Step #1: Create a testSrc/
directory in your module for which you are trying to set up the shared testing code.
Step #2: Put the shared code in that directory (with appropriate subdirectories based on Java package).
Step #3: Add the following closure inside your android
closure in your module's build.gradle
file:
sourceSets {
androidTest {
java.srcDirs <<= 'testSrc'
}
test {
java.srcDirs <<= 'testSrc'
}
}
What this should do is tell Gradle for Android that testSrc
is another source directory for Java code in the androidTest
and test
sourcesets.
Based on CommonsWare's solution and https://code.google.com/p/android/issues/detail?id=181391, the correct way to add an additional class path is to use the +=
or <<=
operators.
sourceSets {
androidTest {
java.srcDirs <<= 'testSrc'
}
test {
java.srcDirs += 'testSrc'
}
}
The solution described in the blog below helped me.
http://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/
I put my shared test code into src/sharedTest/java . Then I added the following code to build.gradle:
<pre>
android {
sourceSets {
String sharedTestDir = 'src/sharedTest/java'
test {
java.srcDir sharedTestDir
}
androidTest {
java.srcDir sharedTestDir
}
}
}
</pre>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With