Skip to content

Instantly share code, notes, and snippets.

@kpgalligan
Last active August 29, 2015 14:07
Show Gist options
  • Save kpgalligan/73ee19719ebc97928758 to your computer and use it in GitHub Desktop.
Save kpgalligan/73ee19719ebc97928758 to your computer and use it in GitHub Desktop.
Testing Setup

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.

  1. Standard JUnit (v3) - This comes with Android
  2. Android JUnit - This also comes with Android, and lets you do stuff with the context and lifecycle.
  3. 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.

What to test

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 Java-only logic

"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.

Tasks

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.

Data loads

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.

Screen flow

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.

Genymotion

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.

Setup

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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment