0% found this document useful (0 votes)
303 views

About This Series of Articles: Testing Android Applications - Part 1

This document provides an overview of testing Android applications, including: 1. It discusses the importance of unit tests and different levels of testing, from isolated logic to UI components. 2. It covers the history of Android testing and how the framework has evolved to better support testing. 3. It provides guidance on setting up the testing environment, including replacing Android dependencies with JUnit and configuring instrumentation testing.

Uploaded by

Shekar S
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
303 views

About This Series of Articles: Testing Android Applications - Part 1

This document provides an overview of testing Android applications, including: 1. It discusses the importance of unit tests and different levels of testing, from isolated logic to UI components. 2. It covers the history of Android testing and how the framework has evolved to better support testing. 3. It provides guidance on setting up the testing environment, including replacing Android dependencies with JUnit and configuring instrumentation testing.

Uploaded by

Shekar S
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Testing Android Applications – part 1

Unit tests and testing are parts of each developer’s life and they are important parts of development.
Developers usually concentrate on unit tests which help them ensure that future changes will not break
the system and let other people do tests and stress tests of applications to reveal bugs, which we as
developers simply cannot see.

About This Series of Articles


We will look at some testing background, discuss basic elements available for you in the SDK and show
how to prepare testing environment to run tests.

Short History of Android Testing


Prior to Android SDK 1.0 there was a huge gap in testing of android applications which inspired 3rd-party
testing frameworks such as Positron and Electron. With Android SDK 1.0 Google filled the gap with its
own testing framework and Positron and Electron frameworks are not needed any more.
Filled the gap in testing and filled it well.
Let’s have a look at what is available there for us. We will concentrate on unit and functional test which
are supported with new part of Android SDK.

Unit Testing
There are basically three levels of tests/unit-test you can use in your application:

1. Unit testing of logic which does not depend on android at all. This is similar to every usual unit
testing. Running tests directly from Eclipse requires some configuration changes which we will cover
later.
2. Unit testing of business logic which depends on Android but does not depend on Android
application elements and UI. This logic does not require activity to be running with complete context
and it can be tested in isolation from UI. This tests usually require something from context (e.g.
resources, configuration, logger, some native classes)
3. Unit/functional testing of Android application elements. These tests are fully instantiated
activities, services, content providers and applications.
Via instrumentation it is possible to send keyboard and touch events to the activities and check
response in UI. It is possible to test lifecycle of a service and to test databases changes made by
content provider.

To be able to test layers of the application in isolation using and fully use previous three levels. It is
useful to follow these guidelines during design and development phases of your Android application:

1. Separate business logic from UI logic as much as possible.


2. Create presenter for complicated activities which models state on the screen and binds to the
views according to MVP pattern.
3. Make clear non expansive contracts between UI layer and business logic.
4. Return error codes from business logic and let present logic transform them to error messages.
5. Pass configuration stored in preferences or in resources in parameters and do not load it in
business logic

How to Prepare Testing Environment for Unit Tests


Android runtime in android.jar contains some subset of JUnit which cannot be used out of android
environment. If you try to run unit test directly from Eclipse, you get exception like this:

Internal Error (classFileParser.cpp:2924), pid=11018, tid=3084700560 # Error: ShouldNotReachHere()

To be able to run tests from Eclipse you have to replace android.jar by JUnit.jar
1. go to „Run Configurations“
2. search for configuration running your unit test
3. go to „Classpath“ tab
4. remove „Android Library“ from „Bootstrap Entries“
5. then go to project build path configuration
6. and add JUnit library to your project
7. you should be able to run JUnit unit tests

Image on the left shows “Run Configuration” dialog with Android library not deleted and image on the
right shows configuration of build path. It is sometimes even possible to unit test classes which depend
on classes from android.jar by deleting JUnit classes in the jar file. But once tested classes require
something from android runtime, you get exceptions.

Setup for Tests Depending on Android


You have to setup instrumentation for your project which allows your tests to hook into your application
and take control over application UI and pass various events.

 Add library definition to elements in your manifest file

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

 Add test runner definition to your manifest file

<instrumentation 
android:name=„android.test.InstrumentationTestRunner“ 
android:targetPackage=„eu.inmite.prj.aclient“ 
android:label=„Tests for Small Applacation“ />
where:
1. android:name is name of the test runner class – use android.test.InstrumentationTestRunner as
default
2. android:targetPackage is application package you want to instrument
3. android:label is name of the test which appears under Instrumentation in Dev Tools application

How to Run Tests Depending on Android in Eclipse and Emulator


To run tests directly from emulator go to DevTools application in the emulator, select Instrumentation
and click on the name of the tests you want to run. Results and lot of debugging output can be found in
Logcat console.

How to Run Tests Depending on Android with adb


To run tests from command line enter:
adb shell am instrument -w PACKAGE/android.test.InstrumentationTestRunner
where PACKAGE is full name of application package which should be instrumented.

Monkey
Does everything seem to be too complicated for you? Do you wonder if there is simple way how to test
Android application? The answer is UI/Application Exerciser Monkey tool available in Android SDK.
Monkey is quite fast way for testing of UI. Just be warned that monkey does not like real devices yet.
There is bug which causes that monkey is killed by Android whenever it tries to send a notification key to
the real T-Mobile G1 device. Now our clever monkey likes to work without bugs with the emulator only.

You might also like