Android Testing Example – Helloworld with ActivityUnitTestCase

November 14, 2012
By

In this article we are going to create an Android Testing Application to test our HelloWorld application discussed here using Android Testing Framework.

The testcase for the HelloWorld application will be created using the class ActivityUnitTestCase which is available in the Android Testing Framework.

This Android testing application is developed in Eclipse ( 4.2.1 ) with ADT plugin ( 20.0.3 ) and Android SDK (R20.0.3).


1. What to Test

In this application, we will test, whether the text displayed in the HelloWorld application is “Helloworld!” or not. If  ”Helloworld!” is displayed, then the testcase returns success other wise it returns failure.


2 Download HelloWorld Application

You can download the HelloWorld application from the given below link, which we are going to test in this article.


3. Extract the downloaded file and Import into Eclipse IDE


4. Create a new Android Testing application project

Create an Android Test Project namely HelloWorldTest

Figure 1 : Create an Android Test Project


5. Enter Project Name

Enter Project name "HelloWorldTest"

Figure 2 : Enter Project name "HelloWorldTest"


6. Select Target project to be tested

Select target application to be tested

Figure 3 : Select target project to be tested


7. Select Build Target for this project

Select build target for this application

Figure 4 : Select build target for this testing project


8. Create a testcase class in the file src/in/wptrafficanalyzer/helloworld/test/MainActivityTest.java with the given below code :


package in.wptrafficanalyzer.helloworld.test;

import in.wptrafficanalyzer.helloworld.MainActivity;
import android.content.Intent;
import android.test.ActivityUnitTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.widget.TextView;

public class MainActivityTest extends ActivityUnitTestCase<MainActivity>{

    // Activity of the Target application
    MainActivity mainActivity;

    // TextView of the MainActivity to be tested
    TextView tvHello;

    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        // Starts the MainActivity of the target application
        startActivity(new Intent(getInstrumentation().getTargetContext(), MainActivity.class), null, null);

        // Getting a reference to the MainActivity of the target application
        mainActivity = (MainActivity)getActivity();

        // Getting a reference to the TextView of the MainActivity of the target application
        tvHello = (TextView) mainActivity.findViewById(in.wptrafficanalyzer.helloworld.R.id.hello_world);
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    @SmallTest
    public void testHello(){
        // The actual text displayed in the textview
        String actual=tvHello.getText().toString();

        // The expected text to be displayed in the textview
        String expected = "Hello world!";

        // Check whether both are equal, otherwise test fails
        assertEquals(expected,actual );
    }
}


9. Running the TestCase

For running the testcase created in the above step, click Run->RunConfigurations menu and create a new configuration for the Android JUnit Test as shown in the given below screen and click Run button

Create and Run new Android JUnit Test Configuration

Figure 5 : Create and Run new Android JUnit Test Configuration


10. Test Result

Test result can be viewed in JUnit pane

Figure 6 : Test result can be viewed in JUnit pane


11. Download


12. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , , ,

4 Responses to Android Testing Example – Helloworld with ActivityUnitTestCase

  1. Sundeep Sharma on December 25, 2013 at 5:32 pm

    Very Basic, good to start but i was expecting some more! like activity button click etc.

  2. rose on July 6, 2015 at 11:29 am

    pls put sample code for unit testing using button click…

  3. rose on July 6, 2015 at 11:29 am

    pls send sample code for unit testing using button click…

  4. g.shiva on November 13, 2015 at 4:32 pm

    error for me please help me
    the error is u need to use theme appcompact theme

Leave a Reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner