Sie sind auf Seite 1von 25

Android Automated Testing

Using JUnit and Robotium

Introduction

What is Junit What is Robotium Tools used Creating a New Test Project Importing Robotium jar Creating a new Test Case Coding convention for JUnit Class

Adapting the Junit Test Case to test Android Applications


Accesing elements Sample Assertions Running Tests

What is JUnit

JUnit is an open source framework designed for writing and running tests in Java programming language It is used too write and run repeatable tests

What is Robotium

Robotium is another testing framework It is designed for writing robust black box tests for Android Applications We can test multiple activities using Robotium The tests are robust The user needs a minimum knowledge about the way the tested application works.

Tools used

We need to import Robotium framework into the project It is used for a accessing graphical elements in a more simple and intuitive way The jar file can be found at

https://code.google.com/p/robotium/downloads/list
We used robotium-solo-4.3.1.jar

Creating a New Test Project


File->New->Other->Android Test Project. Select the project to be Tested

Importing Robotium jar


Main steps:

1. Import robotium-solo- 4.3.1.jar in the following way: TestProjectName -> right click-> Properties -> Java Builder Path -> Add External Jar

2. Search for the download location of robotium-solo-4.3.1.jar


3. On Order and Export tab, check the box corresponding to our imported Jar

Importing Robotium jar

Creating a new Test Class

After creating the test project, in the existing empty package, we need to have a new Junit Test Case.

After the class is generated, it must be changed to work with Android Projects.

Creating a New Test Class


Steps: Define a subclass of TestCase Override the setUp() method to initialize objects under test Override the tearDown() method to release objects under test Define one or more public testAAA() methods that perform a series of commands and assert expected results

1.

2.

3.

4.

Coding convention for JUnit Class

Name of the test class must end with Test Name of the method must begin with test Return type of a test method must be void Test method must not throw any exception Test method must have any parameter

Initial project code

package com.rinf.mobile.rotemps.test; import junit.framework.TestCase; public class ActivityTesting extends TestCase { }

This code will be modified to work for an Android Application. The changes that must be made are displayed in the next slide

Adapting the Junit Test Case to test Android Applications


public class ActivityTesting extends ActivityInstrumentationTestCase2<MainActivity> { Solo solo; public ActivityTesting() { super(MainActivity.class); }

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


protected void tearDown() throws Exception { solo.finishOpenedActivities(); } }

Accesing elements

Click a Button
Radio Button

solo.clickOnButton("Yes"); // the text displayed on the button

solo.clickOnRadioButton(0); // 0 indicates the first element of type Radio // Button existing in the layout

Check Box

solo.clickOnCheckBox(0);

Complete EditText
// text is actually the string that will //be offered as input for the Edit Text

solo.enterText(0, "text");

Accesing elements
This is a way in which we can access a spinner and select one of its
items (in this case the 3rd one)
View view1 = solo.getView(Spinner.class, 0); solo.clickOnView(view1); solo.scrollToTop(); // I put this in here so that it always keeps the list at // start select the 3rd item in the spinner solo.clickOnView(solo.getView(TextView.class, 2));

Check that a message was displayed in a Toast

solo.waitForText("Please insert all required Information")

Accesing elements
There are 2 ways of accessing an element By id
EditText et = (EditText) solo.getView(com.rinf.mobile.rotemps.R.id.etName); solo.enterText(et, "text");

By position
solo.enterText(0, "text");

For a Button we have the option of accessing it by name instead of position


solo.clickOnButton("Yes");

Creating a new test

When creating a new test, usually we add the adnotation @SmallTest The test method must be named in the following way: testName(). If we dont put the keyword test before the actual name, it will not be recognized as a test and so it will be completely ignored.

@SmallTest public void testFieldCompletion() { }

Sample Assertions
Assertions are used to check conditions. Examples of Assertions:

assertEquals (boolean expected, boolean actual)

Asserts that two booleans are equal

assertTrue(java.lang.String message, boolean condition

Asserts that a condition is true.

assertEquals(java.lang.String message, String expecteds, String actuals)


Asserts that two Strings are equal. We can set a specific message for each test to have a small description of the condition to be checked

For complete list see http://junit.sourceforge.net/javadoc/org/junit/Assert.html

Running Tests
Right click on Project Name -> Run As -> Android JUnit Test If the test conditions are fulfilled, the JUnit box has the following appearance:

Running Tests
For a failed test we have the possibility of tracing which condition failed

Complete Example of a Test Project


Requirements for a Test Project

Test the page Send CV Complete all the fields Press the Send button Check if a specific text is displayed The last requirement checks the content provider.

The test passes if information from the input corresponds to the information displayed on the second pace

Complete Example of a Test Project


public class SendCvClassTesting extendsActivityInstrumentationTestCase2<SendCV> { Solo solo; EditText nameET; public SendCvClassTesting() { super(SendCV.class); } protected void setUp() throws Exception { solo = new Solo(getInstrumentation(), getActivity()); nameET = (EditText) solo.getView(com.rinf.mobile.rotemps.R.id.etName); }

protected void tearDown() throws Exception { solo.finishOpenedActivities(); }

Complete Example of a Test Project


@SmallTest public void testFieldCompletion() { solo.enterText(nameET, "text"); solo.enterText(1, " my address"); solo.enterText(2, "this is my email"); solo.enterText(3, "0179"); solo.clickOnCheckBox(0); solo.clickOnButton("Send"); solo.clickOnButton("Yes"); assertTrue("Could not find message list!", solo.searchText("text")); }

Questions ?

Useful online resources


http://robotiumsolo.blogspot.ro/search/label/Toggle%20Button http://www.slideshare.net/mobilemarch/robotium-tutorial http://blog.3pillarglobal.com/mobile-testing-automated-testing-androidrobotium http://www.qualitytesting.info/profiles/blog/show?id=2064344%3ABlogPost%3 A354479

Das könnte Ihnen auch gefallen