48
Unit and Functional Testing for the Android Platform Christopher M. Judd Saturday, February 19, 2011

Unit and Functional Testing for Android Platform

Embed Size (px)

Citation preview

Page 1: Unit and Functional Testing for Android Platform

Unit and Functional Testing for the

Android Platform

Christopher M. Judd

Saturday, February 19, 2011

Page 2: Unit and Functional Testing for Android Platform

Christopher M. JuddPresident/Consultant of

leader

Columbus Developer User Group (CIDUG)

Saturday, February 19, 2011

Page 3: Unit and Functional Testing for Android Platform

Remarkable Ohio

Free

Developed for eTech Ohio and Ohio Historical CenterSaturday, February 19, 2011

Page 4: Unit and Functional Testing for Android Platform

University System Of Ohio

FreeDeveloped for eTech Ohio and University System Of Ohio

Saturday, February 19, 2011

Page 5: Unit and Functional Testing for Android Platform

How many of you are currently or have developed applications for the

Android Platform?

Saturday, February 19, 2011

Page 6: Unit and Functional Testing for Android Platform

How many of you have ever unit or functionally tested your

Android application?

Saturday, February 19, 2011

Page 7: Unit and Functional Testing for Android Platform

How many of you have ever unit tested on

another platform?

Saturday, February 19, 2011

Page 8: Unit and Functional Testing for Android Platform

Why aren’t you testing your Android applications?

Saturday, February 19, 2011

Page 9: Unit and Functional Testing for Android Platform

Testing is the key to

Saturday, February 19, 2011

Page 10: Unit and Functional Testing for Android Platform

Testing is the key to

AgilitySaturday, February 19, 2011

Page 11: Unit and Functional Testing for Android Platform

Unit Testing

Saturday, February 19, 2011

Page 12: Unit and Functional Testing for Android Platform

Unit Testing Basics

Saturday, February 19, 2011

Page 13: Unit and Functional Testing for Android Platform

Why Unit Test?

Improves designFacility change and refactoringSimplifies integrationProvides executable documentation

Saturday, February 19, 2011

Page 14: Unit and Functional Testing for Android Platform

includes

Saturday, February 19, 2011

Page 15: Unit and Functional Testing for Android Platform

Getting Started

Saturday, February 19, 2011

Page 16: Unit and Functional Testing for Android Platform

1.Create Android Test Project

Saturday, February 19, 2011

Page 17: Unit and Functional Testing for Android Platform

Create Android Test Project

Saturday, February 19, 2011

Page 18: Unit and Functional Testing for Android Platform

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.notepad.test" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name">

<uses-library android:name="android.test.runner" /> </application> <uses-sdk android:minSdkVersion="3" />

<instrumentation android:targetPackage="com.example.android.notepad" android:name="android.test.InstrumentationTestRunner" /> <uses-sdk android:targetSdkVersion="4" /> </manifest>

Saturday, February 19, 2011

Page 19: Unit and Functional Testing for Android Platform

Running Unit Tests

Saturday, February 19, 2011

Page 20: Unit and Functional Testing for Android Platform

Running

Run As > Android JUnit Test

Saturday, February 19, 2011

Page 21: Unit and Functional Testing for Android Platform

Writing Unit Tests

Saturday, February 19, 2011

Page 22: Unit and Functional Testing for Android Platform

Test Framework

Instrumentation controls an Android component independently of its

normal lifecycle.

Saturday, February 19, 2011

Page 23: Unit and Functional Testing for Android Platform

TestCases

Saturday, February 19, 2011

Page 24: Unit and Functional Testing for Android Platform

Mocks

Saturday, February 19, 2011

Page 25: Unit and Functional Testing for Android Platform

Functional Testing

Saturday, February 19, 2011

Page 26: Unit and Functional Testing for Android Platform

Monkey RunnerMonkeyRobotium

Saturday, February 19, 2011

Page 27: Unit and Functional Testing for Android Platform

MonkeyRunner

Saturday, February 19, 2011

Page 28: Unit and Functional Testing for Android Platform

functional testing framework for Android applications and devices

Saturday, February 19, 2011

Page 29: Unit and Functional Testing for Android Platform

Saturday, February 19, 2011

Page 30: Unit and Functional Testing for Android Platform

monkeyrunner add_note.py

Saturday, February 19, 2011

Page 31: Unit and Functional Testing for Android Platform

# Imports the monkeyrunner modulesfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage

# Connect to the current devicedevice = MonkeyRunner.waitForConnection()

# Install packagedevice.installPackage('bin/NotePad.apk')

# Run activitydevice.startActivity(component='com.example.android.notepad/.NotesList')

# Press the Menu buttondevice.press('KEYCODE_MENU','DOWN_AND_UP')

# Press Add Note menu itemdevice.press('KEYCODE_A','DOWN_AND_UP')

# Type "Mobidevdays" device.press('KEYCODE_M','DOWN_AND_UP')device.press('KEYCODE_O','DOWN_AND_UP')device.press('KEYCODE_B','DOWN_AND_UP')device.press('KEYCODE_I','DOWN_AND_UP')device.press('KEYCODE_D','DOWN_AND_UP')device.press('KEYCODE_E','DOWN_AND_UP')device.press('KEYCODE_V','DOWN_AND_UP')device.press('KEYCODE_D','DOWN_AND_UP')device.press('KEYCODE_A','DOWN_AND_UP')device.press('KEYCODE_Y','DOWN_AND_UP')

# Press the Menu buttondevice.press('KEYCODE_MENU','DOWN_AND_UP')

# Press save menu itemdevice.press('KEYCODE_S','DOWN_AND_UP')

# Take snapshotscreenshot = device.takeSnapshot()

# Write snapshot to file systemscreenshot.writeToFile('notes_list.png','png')

Saturday, February 19, 2011

Page 32: Unit and Functional Testing for Android Platform

When things don’t work

Saturday, February 19, 2011

Page 33: Unit and Functional Testing for Android Platform

When things don’t work

MonkeyRunner.sleep(1)

add

Saturday, February 19, 2011

Page 34: Unit and Functional Testing for Android Platform

automates android applicationcan run in the simulator or the device

difficult to write scriptsno red bar/green barno verification (other than screenshots)very little documentation

Saturday, February 19, 2011

Page 35: Unit and Functional Testing for Android Platform

Monkey

Saturday, February 19, 2011

Page 36: Unit and Functional Testing for Android Platform

random click stress tester

Saturday, February 19, 2011

Page 37: Unit and Functional Testing for Android Platform

adb shell monkey -p com.example.android.notepad -v 500

Saturday, February 19, 2011

Page 38: Unit and Functional Testing for Android Platform

child proofs our applooks for crashesidentifies unresponsiveness

not sure the real value

Saturday, February 19, 2011

Page 39: Unit and Functional Testing for Android Platform

Robotium

Saturday, February 19, 2011

Page 40: Unit and Functional Testing for Android Platform

Selenium for Android

http://code.google.com/p/robotium/

Open Source

Saturday, February 19, 2011

Page 41: Unit and Functional Testing for Android Platform

1. Create Android Test Project2. Add robotium-solo-x.x.jar

Setup

Saturday, February 19, 2011

Page 42: Unit and Functional Testing for Android Platform

public class NotePadTest extends ActivityInstrumentationTestCase2<NotesList> {

private static final int TWO_SECONDS = 2000; private Solo solo; public NotePadTest() { super("com.example.android.notepad", NotesList.class); }

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

public void testAddNote() throws Exception {

solo.sendKey(Solo.MENU); solo.sendKey(Solo.MENU); // issue 61 solo.clickOnMenuItem("Add note"); solo.sleep(TWO_SECONDS); EditText note = (EditText) solo.getView(R.id.note); solo.clickOnView(note); solo.enterText(note, "Mobidevdays"); solo.sendKey(Solo.MENU); solo.sendKey(Solo.MENU); // issue 61 solo.clickOnMenuItem("Save");

assertTrue(solo.searchText("Mobidevdays")); }

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

}

Saturday, February 19, 2011

Page 43: Unit and Functional Testing for Android Platform

Running

Run As > Android JUnit Test

Saturday, February 19, 2011

Page 44: Unit and Functional Testing for Android Platform

$ adb shell am instrument -w com.example.android.notepad.test/android.test.InstrumentationTestRunner

com.example.android.notepad.NotePadTest:.Test results for InstrumentationTestRunner=.Time: 14.517

OK (1 test)

Command-line

Saturday, February 19, 2011

Page 45: Unit and Functional Testing for Android Platform

JUnit basedred bar/ green barasserts

can run in the simulator or the devicecommand-line automationintegrates with cucumber

little documentationnot approachable by traditional testers

Saturday, February 19, 2011

Page 46: Unit and Functional Testing for Android Platform

Android Resources

http://developer.android.com

Saturday, February 19, 2011

Page 47: Unit and Functional Testing for Android Platform

President/Consultant/Authoremail: [email protected]: www.juddsolutions.comblog: juddsolutions.blogspot.comtwitter: javajudd

Christopher M. Judd

Saturday, February 19, 2011

Page 48: Unit and Functional Testing for Android Platform

http://www.organicdesign.co.nz/File:Warning.svg

Attributions

http://www.flickr.com/photos/heliotrop3/4310957752/

Saturday, February 19, 2011