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

Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

  • Upload
    others

  • View
    18

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Unit and Functional Testing for the

Android Platform

Christopher M. Judd

Saturday, February 19, 2011

Page 2: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Christopher M. JuddPresident/Consultant of

leader

Columbus Developer User Group (CIDUG)

Saturday, February 19, 2011

Page 3: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Remarkable Ohio

Free

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

Page 4: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

University System Of Ohio

FreeDeveloped for eTech Ohio and University System Of Ohio

Saturday, February 19, 2011

Page 5: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

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 the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

How many of you have ever unit or functionally tested your

Android application?

Saturday, February 19, 2011

Page 7: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

How many of you have ever unit tested on

another platform?

Saturday, February 19, 2011

Page 8: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Why aren’t you testing your Android applications?

Saturday, February 19, 2011

Page 9: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Testing is the key to

Saturday, February 19, 2011

Page 10: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Testing is the key to

AgilitySaturday, February 19, 2011

Page 11: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Unit Testing

Saturday, February 19, 2011

Page 12: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Unit Testing Basics

Saturday, February 19, 2011

Page 13: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Why Unit Test?

Improves designFacility change and refactoringSimplifies integrationProvides executable documentation

Saturday, February 19, 2011

Page 14: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

includes

Saturday, February 19, 2011

Page 15: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Getting Started

Saturday, February 19, 2011

Page 16: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

1.Create Android Test Project

Saturday, February 19, 2011

Page 17: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Create Android Test Project

Saturday, February 19, 2011

Page 18: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

<?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 the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Running Unit Tests

Saturday, February 19, 2011

Page 20: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Running

Run As > Android JUnit Test

Saturday, February 19, 2011

Page 21: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Writing Unit Tests

Saturday, February 19, 2011

Page 22: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Test Framework

Instrumentation controls an Android component independently of its

normal lifecycle.

Saturday, February 19, 2011

Page 23: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

TestCases

Saturday, February 19, 2011

Page 24: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Mocks

Saturday, February 19, 2011

Page 25: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Functional Testing

Saturday, February 19, 2011

Page 26: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Monkey RunnerMonkeyRobotium

Saturday, February 19, 2011

Page 27: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

MonkeyRunner

Saturday, February 19, 2011

Page 28: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

functional testing framework for Android applications and devices

Saturday, February 19, 2011

Page 29: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Saturday, February 19, 2011

Page 30: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

monkeyrunner add_note.py

Saturday, February 19, 2011

Page 31: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

# 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 the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

When things don’t work

Saturday, February 19, 2011

Page 33: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

When things don’t work

MonkeyRunner.sleep(1)

add

Saturday, February 19, 2011

Page 34: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

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 the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Monkey

Saturday, February 19, 2011

Page 36: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

random click stress tester

Saturday, February 19, 2011

Page 37: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

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

Saturday, February 19, 2011

Page 38: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

child proofs our applooks for crashesidentifies unresponsiveness

not sure the real value

Saturday, February 19, 2011

Page 39: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Robotium

Saturday, February 19, 2011

Page 40: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Selenium for Android

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

Open Source

Saturday, February 19, 2011

Page 41: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

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

Setup

Saturday, February 19, 2011

Page 42: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

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 the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Running

Run As > Android JUnit Test

Saturday, February 19, 2011

Page 44: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

$ 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 the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

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 the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

Android Resources

http://developer.android.com

Saturday, February 19, 2011

Page 47: Unit and Functional Testing for the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

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 the Android Platform · 2016-10-17 · Unit Testing Basics Saturday, February 19, 2011. Why Unit Test? Improves design Facility change and refactoring

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

Attributions

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

Saturday, February 19, 2011