Android Testing Tutorial


The Android framework includes an integrated testing framework that helps you test all aspects of your application and the SDK tools include tools for setting up and running test applications. Whether you are working in Eclipse with ADT or working from the command line, the SDK tools help you set up and run your tests within an emulator or the device you are targeting.

Test Structure

Android's build and test tools assume that test projects are organized into a standard structure of tests, test case classes, test packages, and test projects.
Android Testing Tutorial

Testing Tools in android

There are many tools that can be used for testing android applications. Some are official like Junit,Monkey and some are third party tools that can be used to test android applications. In this chapter we are going to explain these two tools to test android applications.
  1. JUnit
  2. Monkey

JUnit

You can use the JUnit TestCase class to do unit testing on a class that doesn't call Android APIs. TestCase is also the base class for AndroidTestCase, which you can use to test Android-dependent objects. Besides providing the JUnit framework, AndroidTestCase offers Android-specific setup, teardown, and helper methods.
In order to use TestCase, extend your class with TestCase class and implement a method call setUp(). Its syntax is given below:
public class MathTest extends TestCase {
protected double fValue1;
protected double fValue2;

protected void setUp() {
fValue1= 2.0;
fValue2= 3.0;
}
}
For each test implement a method which interacts with the fixture. Verify the expected results with assertions specified by calling assertTrue(String, boolean) with a boolean.
public void testAdd() {
double result= fValue1 + fValue2;
assertTrue(result == 5.0);
}
The assert methods compare values you expect from a test to the actual results and throw an exception if the comparison fails.
Once the methods are defined you can run them. Its syntax is given below:
TestCase test= new MathTest("testAdd");
test.run();

Monkey

The UI/Application Exerciser Monkey, usually called "monkey", is a command-line tool that sends pseudo-random streams of keystrokes, touches, and gestures to a device. You run it with the Android Debug Bridge (adb) tool.
You use it to stress-test your application and report back errors that are encountered. You can repeat a stream of events by running the tool each time with the same random number seed.

MONKEY FEATURES

Monkey has many features, but it can be all be summed up to these four categories.
  1. Basic configuration options
  2. Operational constraints
  3. Event types and frequencies
  4. Debugging options

MONKEY USAGE

In order to use monkey , open up a command prompt and just naviagte to the following directory.
android->sdk->platform-tools
Once inside the directory , attach your device with the PC , and run the following command.
adb shell monkey -v 100
This command can be broken down into these steps.
  • adb - Android Debug Bridge. A tool used to connect and sends commands to your Android phone from a desktop or laptop computer.
  • shell - shell is just an inteface on the device that translates our commands to system commands.
  • monkey - monkey is the testing tool.
  • v - v stands for verbose method.
  • 100- it is the frequency conut or the number of events to be sent for testing.
This is also shown in the figure:
Android Testing Tutorial
In the above command, you run the monkey tool on the default android UI application. Now in order to run it to your application , here what you have to do.
First run the example code given in the example section in your device. After running , follow the steps of monkey usage and finally type this command.
adb shell monkey -p com.example.test -v 500
This has also been shown in the figure below. By typing this command , you are actually generating 500 random events for testing.
Android Testing Tutorial

Example

The below example demonstrates the use of Testing. It crates a basic application which can be used for monkey.
To experiment with this example , you need to run this on an actual device and then follow the monkey steps explained in the beginning.
StepsDescription
1You will use Eclipse IDE to create an Android application and name it as Test under a package com.example.test. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2Modify src/MainActivity.java file to add Activity code.
3Modify layout XML file res/layout/activity_main.xml add any GUI component if required.
4Create src/MainActivity2.java file to add Activity code.
5Modify layout XML file res/layout/activity_main_activity2.xml add any GUI component if required.
6Modify res/values/string.xml file and add necessary string components.
7Run the application and choose a running android device and install the application on it and verify the results.
Here is the content of src/com.example.test/MainActivity.java.
package com.example.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }


   public void activity2(View view){
      Intent intent = new Intent(this,com.example.test.MainActivity2.class);
      startActivity(intent);
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
}
Here is the content of src/com.example.test/MainActivity2.java.
package com.example.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

public class MainActivity2 extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_activity2);
   }

   public void activity1(View view){
      Intent intent = new Intent(this,com.example.test.MainActivity.class);
      startActivity(intent);
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main_activity2, menu);
      return true;
   }

}
Here is the content of activity_main.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="87dp"
      android:text="@string/test1"
      android:textAppearance="?android:attr/textAppearanceLarge" />

   <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:onClick="activity2"
      android:text="@string/go2" />
</RelativeLayout>
Here is the content of activity_main_activity2.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity2" >

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="125dp"
      android:text="@string/test2"
      android:textAppearance="?android:attr/textAppearanceLarge" />

   <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:onClick="activity1"
      android:text="@string/go1" />


</RelativeLayout>
Here is the content of Strings.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>

   <string name="app_name">test</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>
   <string name="test1">This is activity 1</string>
   <string name="test2">This is activity 2</string>
   <string name="go1">Go to activity 1</string>
   <string name="go2">Go to activity 2</string>
   <string name="title_activity_main_activity2">MainActivity2</string>

</resources>
Here is the content of AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.test"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="14" />

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <activity
         android:name="com.example.test.MainActivity"
         android:label="@string/app_name" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
      <activity
         android:name="com.example.test.MainActivity2"
         android:label="@string/title_activity_main_activity2" >
      </activity>
   </application>
</manifest>
Let's try to run your Anroid Testing application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Before starting your application, Eclipse will display following window to select an option where you want to run your Android application.
Anroid Testing Tutorial
Select your mobile device as an option and then check your mobile device which will display application screen. Now just follow the steps mentioned at the top under the monkey section in order to peform testing on this application.

Comments

Popular Posts