Robotium: black-box testing for Android apps

Arno den Hond

As pointed out in an earlier post the importance of testing can not be understated.
In this post we will delve into BDD of Android apps.
There are a number of other testing tools for Android out there, such as Robolectric and Calculon. Robolectric improves the speed of running the test by executing it outside of the emulator. Calculon is a DSL for testing views and activities. As Robotium seems to most mature and reliable, it is my preference.

Robotium.org

What is Robotium & what does it do?

Robotium is a test framework created to make it easy to write powerful and robust automatic black-box test cases for Android applications. With the support of Robotium, test case developers can write function, system and acceptance test scenarios, spanning multiple Android activities.

In essence, a Robotium test is a subclass of junit.framework.TestCase (an ActivityInstrumentationTestCase2, in this "case") in which, by use of the Robotium library, a Solo object is created which allows easy access to the views in your activities.
Through the solo object, you can set values in input fields, click on buttons and get results from other UI components. Methods of JUnits Assert class can then be used to check those results.

How to set up Robotium?

After creating an Android Test project in the usual way (I will assume you have done that before), you need to add the robotium JAR to the Libraries on the projects Build Path.

How to use Robotium?

In the setUp method of your testing class, you can create a Solo object and specify the Activity to be started.
The Solo object can then be used in your testXXX methods to get and set values in/from UI components and click buttons and such.
Many of Solos methods to access UI components take an int representing the index of the component in the UI. This can lead to brittle code so using getView (and the id defined in the layout xml) is a much safer alternative.
Do keep in mind that changing values and performing clicks is not allowed from outside of the thread that originally created the views, these things must still be done through Solos methods such as setXXX, EnterText and clickOnXXX.

The values to be asserted can then be retrieved through Solos methods isXXX, getCurrentXXX, searchText and others.
Solos finalize method should be called from the tests tearDown method.

An example:

package xebia.addapptesting;

import xebia.addapp.AddApp;
import xebia.addapp.R;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.EditText;

import com.jayway.android.robotium.solo.Solo;

public class AddTest extends ActivityInstrumentationTestCase2<AddApp> {

    private Solo solo;

    public AddTest() {
        super("xebia.addapp", AddApp.class);
    }

    @Override
    protected void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }

    public void testAdd() {
        int one = 1;
        int other = 2;

        solo.enterText((EditText) solo.getView(R.id.one), Integer.toString(one));
        solo.enterText((EditText) solo.getView(R.id.other), Integer.toString(other));
        solo.clickOnView(solo.getView(R.id.calcbutton));

        assertTrue(solo.searchText(Integer.toString(one + other)));
    }

    @Override
    public void tearDown() throws Exception {
        try {
            solo.finalize();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        getActivity().finish();
        super.tearDown();
    }
}

When running the test case on the emulator or an actual device, you will see the values being entered into the UI components or see them being clicked as if you were doing it yourself. After the tests are completed, the JUnit view in Eclipse will show which tests have failed and passed.

Conclusion

I think it looks pretty cool to see the app being run and the values being entered. This kind black-box behavior testing on the emulator is much more realistic than just a unit test.

Stay tuned for my next post on a surprise topic, soon!

Comments (10)

  1. Chandrasekaran - Reply

    November 26, 2011 at 9:07 am

    Can you please explain the code.. I am a android tester.. it will be useful for me..

    My mail id is: gchandrasekaran82@gmail.com

  2. piyush jhawar - Reply

    December 15, 2011 at 9:35 am

    Hello,
    1. can u plz explain how can i make test cases in Robotium ?
    2. i have a project source code now how can i test my app ?

    i tried learning Robotium from 10 days, but i am unable to understand.plz help. i am very confused.

    • siddhartha - Reply

      October 25, 2012 at 1:04 pm

      I myself browsing it too from last week ,, if in case you got to learn it ,, please help me out too.

  3. piyush jhawar - Reply

    December 15, 2011 at 9:37 am

    My mail id is

    pshjhwr@gmail.com

  4. Android Package Synergy | The Agile Radar - Reply

    December 15, 2011 at 7:08 pm

    [...] Package Synergy November 14, 2011By Arno den HondUnlike announced in my previous post this one is neither soon nor on a surprise topic. It is about a general aspect of Android that is, [...]

  5. PraveshSingh - Reply

    December 30, 2011 at 12:36 pm

    Very informative post. Its really helpful for me and beginner too. Check out this link too its also having a nice post with wonderful explanation on black box testing....
    http://mindstick.com/Articles/a8575a5b-5725-4e88-8196-e1e4afa8a194/?Black%20Box%20Testing

    Thanks

    • Demetri - Reply

      October 1, 2015 at 10:28 pm

      Would you elaborate on how to write test cases for an apiatclpion having multiple activities/services. Should we extend the test case classes from android.test.ActivityInstrumentationTestCase2 or android.test.InstrumentationTestCase? An example would be very handy. Thank you

  6. Sukanta - Reply

    February 27, 2012 at 1:41 pm

    Hi all,

    I try to run robotium from the ubuntu terminal and its works fine, but now i want to pass the arguments (like, wants to run the test 100 times or 200 times) at the time of running from the command line(terminal). How can i do that using ubuntu terminal??

  7. Dinesh - Reply

    February 29, 2012 at 2:26 pm

    We need source code of apk file which we want to automate for using R.id.one
    code in our robotium tests.

  8. wangke - Reply

    March 27, 2012 at 3:55 pm

    hello,

    Could you please explain how the robotium to test an apk when does not the source code of the apk?

Add a Comment