Testing Android with “Android Test Project” (Android JUnit)

This article opens a series covering different aspects of application testing on Android platform.

Background

Android platform comes with preintegrated JUnit 3.0 framework. That means you don’t need to install anything in addition to your Android platform and developement environment. In this article I will focus  on simplest testing scenario.
I assuming that we have last version Android platform and Eclipse ADT plugin installed (which at this time are Android 2.1 and ADT 9.5), all software is functioning well (e.g. you can create and run Android applications on emulator and device connected via USB) , and our target version for Android application will be 1.5 with add-on (this is just to be compatible with the most of Android devices currently available on market).

Testing application using Android JUnit

In simplest case all you need to do — is to create Android Test Project in Eclipse with ADT:

  • Click File->New->Other
  • Choose: Android -> Android Test Project
  • In ‘New Project Dialog” choose your project in ‘An existing Andoid project’. Once you did it – the rest of dialog fields will populate automatically
  • Once project is created – start writing you test cases

To create simplest test case:

  • Click File->New->Other
  • Choose:  Java -> JUnit -> JUnit Test Case
  • In ‘New JUnit Test Case’ dialog choose ‘New JUnit 3 test’
  • Once test case class is created – write your testing logic there, you shouldhave something like this:
package kroz.android.app;
import static org.junit.Assert.assertEquals;
import junit.framework.TestCase;

public class MyClassTest extends TestCase {
 protected void setUp() throws Exception {
 super.setUp();
 }

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

/**
 * Custom test method -- created automatically, based on methods available in your target test class
 */
 public void testOnPreExecute() {
// Add your testing logic here
 fail("Not yet implemented");
 }
}

Note: this is a simplest example just to demonstrate main steps on creating testing project and test cases skeletons for testing application on Adroid platform. It doesn’t cover specifics of testing Android application such access to application resources or running test in UI worker thread. I will cover these things in the next chapters

About these ads

One thought on “Testing Android with “Android Test Project” (Android JUnit)

  1. Good post.

    What is the best name for a test case? I was thinking that I should use the name of my prject followed by “TestCase”, e.g. MyProjectTestCase.

    Should there be one TestCase for each class, or is there another way of naming?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s