GUI testing with Instrumentation in Android

Sara picture Sara · Apr 20, 2010 · Viewed 8.9k times · Source

I want to test my Android applications UI, with keyevents and pressed buttons and so on. I've read som documentation that Instrumentation would be able to use for this purpose.

Anyone with expericence with using Instrumentation for UI testing?

Answer

Matthias picture Matthias · Feb 9, 2012

The officially recommended way to perform UI tests on Android is instrumentation, yes. Have a look at InstrumentationTestRunner. There exist wrappers for this sort of functionality which make it a little less painful to use, one of these is Robotium, another is Calculon.

However, most people seem to agree these days that Google's test framework is a fail. It's very flaky, very slow, and the APIs are terrible, making tests difficult to write and understand. Hence, most people I know that run larger test suites opt for Robolectric, which takes UI testing away from the device and Dalvik to a plain old JVM. It has come a long way, and is actually very usable these days. Check it out. The main drawback is of course that it won't actually instrument the app on a device or even render the UI. It makes assertions on code level, so it's not the right choice for black box tests.

Another way to black box / end-to-end test your app is Selenium + NativeDriver. NativeDriver is an implementation of the WebDriver APIs, so you can run Selenium style tests against your Android devices.

One more tool to mention is Android's own monkeyrunner (not the Monkey UI exerciser, which simply sends random events to a device, making it useful for stress testing, but not for functional testing). monkeyrunner is a Python scripted device bridge, against which you can send keystrokes and taps in order to instrument your app. Again, I wouldn't recommend using it though, since it's riddled with bugs and has very limited functionality. It can do other things though, such as taking screenshots of your app under test.