Tuesday, June 16, 2009

Android: Testing on the Android platform - Running tests

This document can be read in Google Docs (http://docs.google.com/View?id=ddwc44gs_184ftgt73d9), cut and paste link.

This post is about Running Tests on the Android platform. We have analyzed how to create tests before and we will be updating the subject soon covering android SDK 1.5_r2 but now how to run these tests is our target. In future installments of this series of articles we will be analyzing unit and functional tests and the tools introduced here will be helpful depending on the case.

There are at least three different complementary ways of running tests, each with its pros and cons. Latest Android ADT plugin supports running tests from inside Eclipse with no additional changes or requirements simplifying the matter a lot compared with previous versions.

Test can be run:

  • from inside Eclipse

  • from the android shell, using am

  • from inside the emulator using Development Tools -> Instrumentation

There's also different methods of including your tests, one method can be including tests inside the same project and the other is creating a parallel project comprising only the tests. The latter gives the advantage of being easily removable from the final APK, as they are part of a different project. Thus this is the one we will be using in future examples.

Running tests from Eclipse

Latest Android ADT plugin (0.9.1) supports running tests from Eclipse. Once you have your tests created inside a separate project select the root project folder and Run as... -> Android JUnit Test.

This video shows this option.

In this case, the obvious advantage is how clearly you can analyze tests success or failure.

Running tests from command line

Tests can be run from the command line using the android shell and am instrumentation command. This has the clear disadvantage of having to type a quite complex command line and if you are already inside android shell, its poor command line editing and history capabilities won't help you a lot. On the other hand, this is scriptable, so probably you have to type it once and then use some kind of script to run your tests.

You will need something like this:

# am instrument -w -e class com.codtech.android.training.sample.project.tests.SampleTest\#testSimpleCreate com.codtech.android.training.sample.project.tests/android.test.InstrumentationTestRunner

Test results for InstrumentationTestRunner=.
Time: 1.14

OK (1 test)

This video illustrates this method:

Running from Instrumentation

On the emulator, Dev Tools -> Instrumentation provides yet another alternative of running your tests.

This video illustrates this method:

To see the results you need to go to DDMS Logcat view in Eclipse.


Depending on the case each particular tool has its own pros and cons. If you are running or re-running a test that has just failed perhaps running from Eclipse is more adequate, if you are running a big test suite and want in some way to automate it, the command line and some scripts could be better. And finally if you are running some functional tests and want to see them while they perform perhaps Dev Tools -> Instrumentation is better.
What's important to notice is that you can use any one on the same set of test suites depending on the case.


Robesz said...

Thanks! This is a good quick review of the unit test possibilities on Android. I hope every Android developer will write tests for their apps, I think it is very useful.

Arjun said...

Hi, can I know how to access Development Tools - Instrumentation in Android Emulator

Diego Torres Milano said...

Development Tools -> Instrumentation works in emulator too. What's the problem ?