Testing in Android : Part 1 : Unit Testing

**Why Android Unit Testing is used : **

Unit testing is used to test each of the smallest testable parts (units) individually and independently . By using unit tests, we can easily verify that logic of the individual units is correct. i.e. we should run unit tests after every build to detect software regressions. For android application unit testing, we can create two types of unit tests :

_Local Tests : _Run locally on JVM . Less execution time : Use this approach if test units have no dependencies on Framework . Some dependencies can be removed by using mock objects. (will discuss this later) _ Instrumented Tests: _Unit tests that run on Android Device or Emulator.

**Project Configuration For Local Unit Tests : **Android testing is based on JUnit. JUnit is a unit testing framework to write repeatable tests. In Android Studio, you must store your Local unit tests files under directory. If you are using different flavor or build types, you should keep your unit tests separately for each flavor.

 

table


Dependencies :  As mentioned above, Android testing is based on JUnit, first dependency we should use should be JUnit 4 framework . Also if our test cases are dependent on Android Framework , we should include Mockito  library to simplify these test cases. Add the following dependencies to your build.gradle file :

dependencies {
    // Required -- JUnit 4 framework
    testCompile 'junit:junit:4.12'
    // Optional -- Mockito framework
    testCompile 'org.mockito:mockito-core:1.10.19'
}

** Project configuration for Instrumented Unit Tests : **For instrumented unit tests, we must place the source code under “src/androidTest/java” directory.For instrumented unit tests, we need to download Android Testing Support Library package which is included with “Android Support Repository” under android.support.test package . 

To Download Android Support Repository :

  1. Start the Android SDK Manager.

  2. Scroll to the end of the list , and select “Android Support Repository” under “Extras” folder.

  3. Click the “install packages” button.

Now we can include following dependencies to our project :

  1. JUnit 4 test runner (AndroidJUnitRunner) that will help us to run JUnit3 or JUnit4 style test classes. This handles loading test packages, application , running tests and reporting test results.( This class replaces the InstrumentationTestRunner class which supports only JUnit 3 tests.)

  2. APIs for functional UI tests (Espresso and UI Automator)

     Also we can include ”Hamcrest” library to create more flexible assertions. (will discuss it later)

That’s it. Your final build.gradle file should be look like :

dependencies {
    androidTestCompile 'com.android.support:support-annotations:23.0.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    // Optional -- Hamcrest library
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
}

As we are using JUnit4 test classes, we need to specify AndroidJUnitRunner as the default test instrumentation runner as below :

android {
    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

You can see on your android Studio that both java folders under “test” and “androidTest” are marked as green. This Test Artifacts feature ( both unit and instrumentation tests are enabled simultaneously ) is introduced recently. Previously we could work with only one test artifact at a time.

unitTesting-android