Wednesday, December 26, 2012

Instrumentation vs Robotium

What is Instrumentation Testing ?

Android framework includes an integrated testing framework that helps you test all aspects of your application and the SDK tools include tools for setting up and running test applications. 

  • Instrumentation testing allows you to verify a particular feature or behaviour with an automated JUnit Test Case.
  • Instrumentation class allows you to start and stop Activities, run actions on the user interface thread, send key events and more and make assertions about various UI elements.
  • Instrumentation allow you to write test cases to test applications at a much lower level than UI screen shot tests.
  • The ActivityInstrumentationTestCase2 class can be used to test access several Activitities.
  • ActivityInstrumentationTestCase2 allows to use the full Android system infrastructure.

Drawbacks and limitations using Instrumentation:

  • 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
  • Cannot catch UI bugs.
  • Tests run slowly
    • Like if it would be done manually
    • Not suitable for TDD

See the sample code to view the difference between Instrumentation and Robitum implementations

Sample Code :

Instrumentation test code to send the input “1 and 2”, press “Add” button and check the result
   1: // Find views   
   2: EditText num1 = (EditText)   
   3: getActivity().findViewById(com.calculator.R.id.num1);   
   4: EditText num2 = (EditText)   
   5: getActivity().findViewById(com.calculator.R.id.num2);   
   6: Button addButton = (Button)   
   7: getActivity().findViewById(com.calculator.R.id.add_button);   
   8:  
   9: // Perform instrumentation commands  
  10: TouchUtils.tapView(this, num1);  
  11: sendKeys(KeyEvent.KEYCODE_1);  
  12: TouchUtils.tapView(this, num2);  
  13: sendKeys(KeyEvent.KEYCODE_2);  
  14: TouchUtils.tapView(this, addButton);  
  15:   
  16: // Fetch result and compare it against expected value  
  17: String actual = num1.getText().toString();  
  18: String expected = "3.0";  
  19: assertEquals("Addition incorrect", expected,actual);


The above logic can be implemented with Robotium as follows,


   1: public void testAdd() {
   2: solo.enterText(0, "1");
   3: solo.enterText(1,“2");
   4: solo.clickOnButton("Add");
   5: assertTrue(solo.searchEditText(“3.0")); 
   6: }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.