There are many testing libraries for Android, and we'll be adding more as time goes on, but for now we'll focus on the following.
- Standard JUnit (v3) - This comes with Android
- Android JUnit - This also comes with Android, and lets you do stuff with the context and lifecycle.
- Espresso - Built on top of the Android JUnit. Makes instrumentation usable.
Eventually we'll probably get into dependency injection, mocking, etc, but for now I'd like to focus on relatively macro testing.
This is relatively subjective. I don't think testing every piece of logic is a good idea. Much of it is too simple, or is basically covered by higher level tests.
"Complex" is subjective, but we'll start with best judgement until that doesn't quite cut it. If the code does not depend on Android or local file resources, you can use a basic unit test to run it.
Testing tasks should be sufficient for reading/storing operations. We'll need to use some Android junit stuff to get access to the context. Will discuss.
For non-trivial data loads (logic applied to data that came from the db/network), we should write some testing around that. Again, depends on the complexity.
All major screen flows should have tests written to run through them. This kind of testing will be mostly concerned with macro testing of the app's ability not to crash, and make sure that changes don't completely break the existing screens.
We should create and extend test cases to decide what flows should be created.
It should be possible to instrument Genymotion to simulate poor network situations. This will take some research, but its a good way to test network failures. If that isn't so easy to do automatically, we can manufacture this in some with with Retrofit.
Android Studio projects come prepared with basic unit test support, as well as Android specific lifecycle testing. Under 'src', next to 'java', make sure there's an 'androidTest' folder. Under that is 'java', which is where the tests go.
Espresso is slightly more complex, but since it interacts directly with the standard Android unit tests, its pretty simple.
In the main project build file (above 'app'), add the following:
allprojects {
repositories {
jcenter()
maven {
url "http://ci.novoda.com/maven/releases"
}
}
}
In the app's build.gradle, add the following to the dependencies section:
androidTestCompile "com.google.android:android-espresso-bundled:1.1.0-SNAPSHOT"
Up in the 'android>defaultConfig' section, add the following:
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"