Skip to content

Android Tests

Tuong-Nguyen edited this page Apr 14, 2017 · 1 revision

There are 3 types of tests in Android:

  • Local Unit Test: tests does not depend on Android framework API. These tests run fast because it does not require Android device or emulator. Mock can be used for mocking Android classes.

  • Instrumented test: tests which depends on Android system such as Context class, SharedPreference, ...

  • UI tests: tests run the application and interact with the application like user.

Instrumented test:

The tests are put at: module-name*/src/androidTest/java/*

Setup: In your app's top-level build.gradle file, you need to specify these libraries as dependencies:

dependencies {
            androidTestCompile 'com.android.support:support-annotations:24.0.0'
            androidTestCompile 'com.android.support.test:runner:0.5'
            androidTestCompile 'com.android.support.test:rules:0.5'
            // Optional -- Hamcrest library
            androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
            // Optional -- UI testing with Espresso
            androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
            // Optional -- UI testing with UI Automator
            androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}

android {    
    productFlavors {
        // The actual application flavor
        production {
            minSdkVersion 15
        }
        // Test application flavor for uiautomatior tests
        integrationTest {
            minSdkVersion 18
        }
    }

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

Reference: https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html