Android Testing Framework – Testing ListView

November 20, 2012
By

The objective of this article is to develop an Android Test Application to test a listview in an Android application. In this article, we will test the application that we have developed in the article titled “A ListView application in Android“.

In order to test this application, we will use the test case class ActivityInstrumentationTestCase2.

This test 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 appplication, we will test the listview widget available in Android application that we have developed in the article ”A ListView application in Android“.

We test the listview by checking whether all the items supposed to be displayed in the listview are displayed successfully. This is ensured by counting the items displayed in the listview using ActivityInstrumentationTestCase2.


2. Download the application “ListViewDemo” from the given below link


3. Extract the downloaded file and import into Eclipse IDE


4. Create an Android Test Project with the given below details

Project Name : ListViewDemoTest

Location : Default Location

Build Target : Android 4.0 ( API Level 14 )

Test Target : ListViewDemo (The application downloaded in the above step)


5. Create a TestCase class namely MainActivityTest with the given below code in the file src/in/wptrafficanalyzer/listviewdemo/test/MainActivityTest.java


package in.wptrafficanalyzer.listviewdemo.test;

import in.wptrafficanalyzer.listviewdemo.MainActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest;
import android.widget.ListView;

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    /** The activity containing ListView to be tested */
    MainActivity mActivity;

    /** The listview to be tested */
    ListView mListView;

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

    /** Setting up test fixture */
    @Override
    protected void setUp() throws Exception {
        super.setUp();

        /** Getting the reference to the activity containing listview to be tested */
        mActivity = getActivity();

        /** Getting the reference to the activity to be tested */
        mListView = (ListView) mActivity.findViewById(in.wptrafficanalyzer.listviewdemo.R.id.listview);

    }

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

    /** Testing Listview by checking its item count */
    @SmallTest
    public void testListView(){
        int expectedCount = 10;
        int actualCount = mListView.getAdapter().getCount();
        assertEquals(expectedCount, actualCount);
    }
}


6. Run this test project

Screenshot of the application while testing

Figure 1 : Screenshot of the application while testing

 

Showing Test Results

Figure 2 : Showing Test Results


7. Download


8. 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: , , , ,

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