This article is for Robotium beginners.Robotium is a test automation tool for android application programmers.This presentation explains you basics of robotium and writing test cases for all UI components.
2. Session Agenda
⢠Robotium⌠What is it?
⢠Where to get it and how to set it up
⢠âNormalâ Android unit testing background
⢠Why Robotium is needed
⢠Using Robotium
⢠Robotium Tips/Tricks/Issues
⢠Complimentary tools
⢠Further Resources
⢠Q&A
3. Robotium â what is it?
⢠An open source test framework
⢠Used to write black or white box tests (emphasis is on black box)
⢠White box testing â testing software that knows and tests the internal
structures or workings of an application
⢠Black box testing â testing software functionality without knowledge of an
application (perhaps where the source code is not even available)
⢠Tests can be executed on an Android Virtual Device (AVD) or real device
⢠Built on Java (and Android) and JUnit (the Android Test Framework)
⢠In fact, it may be more appropriate to call Robotium an extension to the
Android test framework
4. Robotium Project Setup
⢠Install and setup JDK
⢠Install and setup Eclipse (optional)
⢠Install and setup Android Standard Development Kit (SDK)
⢠Supports Android 1.6 (API level 4) and above
⢠Install and setup Android Development Tools (ADT) for Eclipse (optional)
⢠Create Android AVD or attach device by USB
⢠Create an Android Test Project
⢠Download Robotium JAR and add to project classpath
⢠robotium-solo-X.X.jar (version 3.6 the latest as of this writing)
⢠From code.google.com/p/robotium/downloads/list
5. Background-Android JUnit Testing
⢠Android testing is based on JUnit
⢠You create test suites, classes (test cases), methods
⢠Organize tests into a Android Test project
⢠No annotations
⢠Test case classes can extend good-old-fashion JUnit3 TestCase
⢠To call Android APIs, base class must extend AndroidTestCase
⢠Use JUnit Assert class to check/display test results
⢠Execute tests using an SDK provided InstrumentationTestRunner
⢠android.test.InstrumentationTestRunner
⢠Usually handled automatically via IDE
8. The âAppâ Used to Demo Today
Name should be in format : Last name,First name
Email Should end with â.comâ
Should choose atleast one radio button
Should contain only numbers
Should choose atleast one checkbox
9. Robotium Project Setup
⢠Prerequisites
⢠Install and setup JDK
⢠Install and setup Eclipse (optional)
⢠Install and setup Android Standard Development Kit (SDK)
⢠Supports Android 1.6 (API level 4) and above
⢠Install and setup Android Development Tools (ADT) for Eclipse (optional)
⢠Create Android AVD or attach device by USB
⢠Create an Android Test Project
⢠Download Robotium JAR and add to project classpath
⢠robotium-solo-X.X.jar (version 3.6 the latest as of this writing)
14. Example Robotium commands
public void testAdd() {
solo.enterText(0, "5");
solo.enterText(1,"3");
solo.clickOnButton("Add");
assertTrue(solo.searchEditText("8.0"));
}
15. Tips / Tricks / Issues
Robotium (and all JUnit tests) operate in the
same process (DVM) as the original app
⢠Robotium only works with the activities and views within the defined app
⢠For example: Canât use intent to launch another app and test activity work
from that app
The popup keyboard is accomplished with a
bitmap in Android
⢠Robotium (or any unit test software) doesnât see the âkeysâ as buttons or
anything.
16. Tips / Tricks / Issues
Use waitFor methods liberally.
⢠Especially if new screen opens or changes to what is displayed are
occurring.
⢠The waitFor methods tell Robotium to wait for a condition to happen
before the execution continues.
public void testGoodLogin() {
solo.enterText(0, âusername");
solo.enterText(1, âpassword");
String label = res.getString(R.string.login_button_label); solo.clickOnButton(label);
String title = res.getString(R.string.title_activity_systemv); solo.waitForText(title);
solo.assertCurrentActivity("systemv", SystemVActivity.class); solo.getCurrentActivity().finish();
}
17. Tips / Tricks / Issues
RadioButtons are Buttons, EditText are Text,
etcâŚ
⢠Getting the proper widget by index can be more
difficult
⢠Use of index also makes the test case more brittle
due to potential layout changes
⢠Consider clickOnButton(âClearâ) vs.
clickOnButton(6)
18. Tips / Tricks / Issues
Resources in Android are at a premium
(especially when test cases and App code are
running in same DVM).
⢠Use solo.finishOpenedActivities() in your tearDown method.
⢠It closes all the opened activities.
⢠Frees resources for the next tests
⢠Robotium has some difficulty with animations
⢠Robotium doesnât work with status bar notifications
19. Tips / Tricks / Issues
Black Box Testing (when all you have is the
APK file) is a little more tricky.
⢠Recall in the demo, the test application wants the main activity name?
public TestDataCollectionActivity() {
super(DataCollectionActivity.class);
}
⢠You may not know this for a 3rd party/black box app.
⢠You can get the activity name by loading the APK to an AVD or device, running it, and watching the
logcat.
⢠The APK file has to have the same certificate signature as the test project.
⢠Probably have to delete the signature and then resign the APK with the Android debug key
signature.
â˘Itâs easier than it sounds.
20. Tips / Tricks / Issues
⢠Robotium can automatically take screenshots
⢠solo.takeScreenshot( )
⢠Robotium can be run from the command line (using adb shell)
⢠adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner
⢠Robotium can test with localized strings
⢠solo.getString(localized_resource_string_id)
⢠Code coverage is a bit lackluster at this time
⢠Can be done with special ant task and command line tools
⢠Robotium does not work on Flash or Web apps.
21. Tips / Tricks / Issues
Benefits
â Easy to write, shorter code
â Automatic timing and delays
â Automatically follows current Activity
â Automatically finds Views
â Automatically makes own decisions
â when to scroll etc.
â No modification to Android platform
â Test execution is fast
22. Tips / Tricks / Issues
Required to know implementation details
â You often have to manually add Thread.sleep(500) to make tests work
â Large apps can be very complex to test
â Tests run slowly like if it would be done manually