Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Testing with Eclipse and JUnit

Author: Adrián Santalla
Published? true
FormatLanguage: WikiFormat


You need to create and use a new Test Project to test your Android application.


Here's how to create and use a Test Project:

  • Use your IDE to create a new Android project associated with your Android application project.
  • If necessary, adjust the AndroidManifest.xml file of your Test Project with the necessary lines to test your Android applicaction.
  • And finally, write and run your tests, using the JUnit3 API.


1. Create a new Android Test Project within your Android application project

First of all, you need to create a new Android project with the main application project to store your tests. Either a Project, if you're using Eclipse, or a Module, if you're using IntelliJ. IntelliJ IDEA allows you to nest the Module inside your existing Project; Eclipse does not allow projects to overlap, hence it requires the Android Test Project to be a top-level project. This new project should have an explicit dependency on your main application project. The Eclipse New Android Project wizard will create this and set it up correctly at the time you create the original project, if you remember to click the checkbox.

The image below shows the IDea test project structure. As you can see, the new test project lies within the main application project.

Here is the corresponding Eclipse project structure; two projects.

2. Adjust the AndroidManifest.xml file of the Test Project

Once you have created your new test project, you should verify all values of the AndroidManifest.xml file of the project. It's necessary to set the package name of the main source of the application that you would like to test.

Imagine that you are testing an application which its package name is called my.pkg.app. You should create a test project and your AndroidManifest.xml file should look like the text below:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

        <uses-library android:name="android.test.runner" />

    <instrumentation android:name="android.test.InstrumentationTestRunner"
        android:label="Tests for my.pkg.app"/>

The package attribute of the manifest tag stores the package name of the test project and, the most important thing, the android:targetPackage of the instrumentation tag stores the package name that you would like to test.

Again, the Eclipse Wizard will set this up if you create the test projects associated with the main project, either at the same time, or by selecting the main when creating the test.

3.Write and Run Your Tests

Finally, you can start to write your own tests. The Android testing API is based on the JUnit3 API and provides several types of test classes, like AndroidTestCase, Component-specific test cases, ApplicationTestCase or InstrumentationTestCase. Given how quickly Google cranks out new releases of Android, it is peculiar, and a bit unfortunate, that they haven't yet upgraded to 2005's JUnit 4...

When you create your first test case with your IDE, it is very useful to create a test case that inherits from ActivityInstrumentationTestCase2. This kind of test class allows us to create functional test.

The example below shows a simple functional test:

public class MainTest extends ActivityInstrumentationTestCase2<Main> {

    public MainTest() {
        super("my.pkg.app", Main.class);

    public void test() {
        TextView textView = (TextView) getActivity().findViewById(R.id.textView);

        assertEquals("Hello World!", textView.getText());

The Main class that appears in the test is the Main activity of the main application project. The test constructor uses the main application package name and the class of the main activity. From now on, you can create test cases using the standard methods of the Android API to get a references to the activity elements. In the above test we are testing that the main activity has a TextView with the text "Hello World!" associated.

See Also:



The source code for this project can be downloaded from https://github.com/asantalla/Hello-Android-Testing.


The source code for this project is in the Android Cookbook repository, http://github.com/IanDarwin/Android-Cookbook-Examples/,in the subdirectory HelloTestingTarget.
No records found.