Thursday, November 17, 2011

Robotium - automated unit test tool for Android...

the following text explains how to use Robotium in Eclipse.. Robotium is an automated unit testing tool for Android...
for this we need to download the Robotium.jar file from here...
setting up the environment is easy...

  • put the above jar in the build path’s library section...
  • add the following lines of code in the manifest.xml file in the manifest section (i.e. outside the app section)...
(instrumentation android:name="android.test.InstrumentationTestRunner"
   android:targetPackage="training.android.trainingunitconverter"
   android:label="1" /)

the below snippet of code is to test the UnitConverter app...

  • create a file(say UnitConverterTest.java) and add the following piece of code...

package training.android.trainingunitconverter;

import java.util.ArrayList;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.Smoke;
import android.widget.EditText;
import android.widget.Spinner;
import com.jayway.android.robotium.solo.Solo;

public class UnitConverterTest extends ActivityInstrumentationTestCase2{
private Solo solo;
public UnitConverterTest() {
super("training.android.trainingunitconverter", UnitConverter.class);
}
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
@Smoke
public void testUnitDropDown() throws Exception {

solo.assertCurrentActivity("Expected UnitConverter activity", "UnitConverter");
solo.pressSpinnerItem(0, 1);
solo.sleep(2000);
EditText value = (EditText)solo.getView(R.id.EditTextValue);
solo.enterText(value, "1");
solo.pressSpinnerItem(1,0);
solo.sleep(1000);
solo.pressSpinnerItem(2,1);
solo.sleep(1000);
solo.clickOnButton(0);
assertTrue(solo.searchText("1000.0"));
}
@Override
public void tearDown() throws Exception {
//Robotium will finish all the activities that have been opened
solo.finishOpenedActivities();
}
}

however, it seems that the solo.PressSpinnerItem() is not functioning properly on the emulator (either Android 2.2 or Android 1.6)... hopefully this issue will be fixed in the next release of Robotium...

to run it in the emulator, right click on the Project... go to Run As.. and press  Android JUnit Test...

the screen shot for the above test condition is 

as from the test case
@Smoke
public void testUnitDropDown() it is clear, that we are first asking the test framework to select the item having index 1 (that is Weight) from the first spinner. then we are asking the framework to put a value 1 in the edittext. we are then asking the test framework to select item with 0th index (that is Kg) from the second spinner and item with index 1 (that is gm) from the third spinner... and we are asking the framework to press Convert button... so the expected result will be 1000.0... and we search in the test case if it has the value 1000.0. then the test case will PASS
similarly we can test different scenarios automatically by having more such functions as testUnitDropDown...


note:  the source code of the UnitConverter App with the Robotium test code can be cloned from https://github.com/sommukhopadhyay/UnitConverter/tree/f349925ffbd2cccb3a0bac37c4d7b428d92b354d

No comments: