Testing Android Applications - part 1
![No Image 01 No Image 01](https://www.svetandroida.cz/media/2009/01/No-Image-01.png)
Reklama
Ondra začal psát pro blog Sama Lu (který je například autorem velmi oblíbené aplikace aTrackDog) tuto sérii článků o testování androidích aplikací. Články jsou sice v angličtině, ale jsou natolik zajímavé, že nám přišlo škoda nepodělit se o ně. Ale nebojte, rozhodně to neznamená, že bychom na našem webu začali psát už jen anglicky… 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 This is the first article about testing Android applications. 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. In following articles we will concentrate on describing testing methods in detail. 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:
- 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.
- Unit testing of business logic which depends on Android but does not depend on Android application elements and ui. These logic does not require activity to be running with complete context and it can be tested in isolation from ui. These tests usually require something from context (e.g. resources, configuration, logger, some native classes)
- Unit/funcional 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.
- Separate business logic from ui logic as much as possible.
- Create presenter for complicated activities which models state on the screen and binds to the views according to MVP pattern.
- Make clear non expansive contracts between ui layer and business logic.
- Return error codes from business logic and let present logic transform them to error messages.
- Pass configuraton stored in preferences or in resources in parameters and do not load it in business logic
… 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
- go to „Run Configurations“
- search for configuration running your unit test
- go to „Classpath“ tab
- remove „Android Library“ from „Bootstrap Entries“
- then go to project build path configuration
- and add Junit library to your project
- you should be able to run junit unit tests
![]() |
![]() |
- 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:
- android:name is name of the test runner class – use android.test.InstrumentationTestRunner as default
- android:targetPackage is application package you want to instrument
- android:label is name of the test which appears under Instrumentation in Dev Tools application
adb shell am instrument -w PACKAGE/android.test.InstrumentationTestRunnerwhere 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. Next Article… So far we know what we can test on Android application and we know how to setup and run various kinds of tests. Next time we will look at some tips and hints how to actually write unit and functional tests. Stay tuned…