Building Unit and Instrumental Tests on Android

In Android Studio, Its support 2 type of Testing which are Unit and Instrumental test. What difference between unit and instrumental test? Unit tests is testing a Class or Method for its behaviour and it run on your local machine only. These tests are compiled to run locally on the JVM to minimise execution time.  Instrumental tests is a User Interface testing that run on an Android device or emulator. These tests have access to Instrumentation information such as Context of the application. It also allow to control the life cycle and the event of the activity. In this tutorial, I will teach you how to building unit and instrumental tests on Android project.  We will create the simple unit and instrumental test to test the application.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “InstrumentalUnitTestExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

After you create the project, the android will automatic add the unit and instrumental test in your gradle. There are Junit (Unit Test) and Espresso (Instrumental Test)

Edit class

Open this file and add the sample method for unit testing the method is it working a not. You can follow the below source code.

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

    public static int addNumber(int a,int b){
        return a+b;

Edit class

Modify this class, this class is in your package name “xxx”(androidTest). Add the TestRule so this class will simulate the Activity and test out the method.

public class ExampleInstrumentedTest {

    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);

    public void useAppContext() throws Exception {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.questdot.instrumentalunittestexample", appContext.getPackageName());

    public void shouldBeAbleToLaunch() throws Exception {
        onView(withText("Hello World!")).check(matches(isDisplayed()));

Edit class

Find this class in your package name “xxx”(test). Add Extra method to test the method from MainActivity.

public class ExampleUnitTest {
    public void addition_isCorrect() throws Exception {
        assertEquals(4, 2 + 2);

    public void Test() throws Exception{
        int result =MainActivity.addNumber(1,2);


You can refer the Espresso cheat sheet for the quick reference to perform your test case. This cheat sheet contains most available Matchers, ViewActions and ViewAssertions.

Run Your Test

In conclusion, the unit and instrumental test has been done to implement, you can try it out. For Unit and Instrumental Test, Right click the Class and click Run XXX. It will run the test for the class. You can refer the following picture.

(Building Unit and Instrumental Tests on Android)

Source Code

(Visited 621 times, 1 visits today)

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *