Slide 1

Slide 1 text

Android Application Development: Better, Faster, Stronger!

Slide 2

Slide 2 text

Your hosts Filip Maelbrancke (@fmaelbrancke) Peter Kuterna (@peterkuterna) ! ! Consultants at iDA MediaFoundry (@iDAMediaFoundry) / XTi

Slide 3

Slide 3 text

Why this session?

Slide 4

Slide 4 text

First off... Why? What? How?

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Avoid problems

Slide 9

Slide 9 text

You = McGuyver

Slide 10

Slide 10 text

Deployment pipeline Commit stage Automated test/QA stage Manual QA testing UAT test Production Capacity / load testing

Slide 11

Slide 11 text

Build pipeline Checkout / compile Unit tests Test coverage Code analysis Create deployable artifact Deploy for automatic QA test Trigger automated QA stage

Slide 12

Slide 12 text

IDE Eclipse ! IntelliJ ! Android Studio

Slide 13

Slide 13 text

Build Ant ! Maven ! Gradle

Slide 14

Slide 14 text

CI Jenkins ! Atlassian Bamboo ! Travis ! Teamcity

Slide 15

Slide 15 text

Testing JUnit / TestNG ! Mocking ! UI testing

Slide 16

Slide 16 text

Build system

Slide 17

Slide 17 text

Android project setup & build ant gradle

Slide 18

Slide 18 text

Android SDK build system

Slide 19

Slide 19 text

Gradle? why - goals Powerful build system declarative, flexible tooling API ! Unified across IDEs & CI servers ! Free / open source

Slide 20

Slide 20 text

Basics buildscript { ! repositories { ! mavenCentral() ! } ! dependencies { ! classpath ‘com.android.tools.build:gradle:0.6’ ! }! }! ! apply plugin: ‘android’! ! android { ! compileSdkVersion 19 ! buildToolsVersion ‘19.0.0’! }! !

Slide 21

Slide 21 text

Gradle tasks assemble debug / release ! check (test) check / connectedCheck ! build

Slide 22

Slide 22 text

Build types build / packaging customization debuggable flag ProGuard signing configuration source / resources overlay debug and release prebuilt

Slide 23

Slide 23 text

local dependencies dependencies { compile fileTree(dir: ‘libs’, include: ‘*.jar’) } android { ... }

Slide 24

Slide 24 text

remote dependencies repositories { mavenCentral() } android { ... } dependencies { instrumentTestCompile ‘com.squareup:fest-android:1.0.4’ compile file(‘libs/protobuf.jar’) }

Slide 25

Slide 25 text

multi-project setup MyProject/ | settings.gradle + app/ | build.gradle + libraries/ + lib1/ | build.gradle + lib2/ | build.gradle

Slide 26

Slide 26 text

android library Binary Bundle (.aar) - Uploadable to repositories Support for - assets - ProGuard rules - Custom Lint Rules - ...

Slide 27

Slide 27 text

build variants build variants product flavors

Slide 28

Slide 28 text

Device vs emulator Genymotion

Slide 29

Slide 29 text

Testing

Slide 30

Slide 30 text

Testing Manual testing is tedious ! Humans are not very good at testing ! But computers are...

Slide 31

Slide 31 text

“Computers are designed to do simple repetitive tasks. The second you have humans doing repetitive tasks, all the computers get together late at night and laugh at you...” Neil Ford

Slide 32

Slide 32 text

Manual vs Automated

Slide 33

Slide 33 text

Manual vs Automated

Slide 34

Slide 34 text

Early detection

Slide 35

Slide 35 text

Excuses We never make mistakes! The functionality is trivial Tests slow us down Management won’t let us

Slide 36

Slide 36 text

Unit test Isolated Repeatable Fast Self-documenting

Slide 37

Slide 37 text

Repeatability Tests need to be run by every developer (no matter what development stack he uses) Tests must not rely on the environment in which they are being run

Slide 38

Slide 38 text

Speed The shorter, the less distraction, less of an interruption to our workflow

Slide 39

Slide 39 text

Self documenting Testable code is clear and easy to follow No need to explain how a certain component works, we can just look at the test No need to write documentation

Slide 40

Slide 40 text

Self documenting Testable code is clear and easy to follow No need to explain how a certain component works, we can just look at the test No need to write documentation Tests = usage examples

Slide 41

Slide 41 text

Testing Automated testing

Slide 42

Slide 42 text

(Unit) testing in Android Android SDK (JUnit 3-based) Instrumentation tests ! Monkeyrunner The monkey UIAutomator + UIAutomatorViewer

Slide 43

Slide 43 text

Android instrumentation tests JUnit 3 ! import junit.framework.*; public class MyTestClass extends TestCase { public void testMethodName() throws Exception { } }

Slide 44

Slide 44 text

Android instrumentation tests Instrumentation = set of control methods or “hooks” in the Android system ! These hooks control an Android component independently of the normal lifecycle

Slide 45

Slide 45 text

Android instrumentation tests Application Activity Service Content Provider

Slide 46

Slide 46 text

Android instrumentation tests Isolate tests from the system: MockApplication MockContext MockResources MockContentProvider MockContentResolver MockPackageManager

Slide 47

Slide 47 text

Exercise

Slide 48

Slide 48 text

The monkey Generates pseudo-random stream of user events: clicks touches gestures system-level events ! stress test your app

Slide 49

Slide 49 text

The monkey command-line tool Basic configuration options, such as setting the number of events to attempt. Operational constraints, such as restricting the test to a single package. Event types and frequencies. Debugging options. ! $ adb shell monkey -p your.package.name -v 500 http://developer.android.com/tools/help/monkey.html

Slide 50

Slide 50 text

Exercise

Slide 51

Slide 51 text

UI testing UIAutomator ! ! ! ! Espresso

Slide 52

Slide 52 text

Third Party Tools

Slide 53

Slide 53 text

Robolectric Android core libraries depend upon the actual Android operating system

Slide 54

Slide 54 text

Robolectric AndroidTestCase needs an instance of the emulator to run ! Big time sink: - spin up emulator - deploy the APK - run the actual tests

Slide 55

Slide 55 text

Robolectric Replaces the behavior of code that would otherwise need an emulator / device Write JUnit tests without the baggage of a device ! Does this by using Shadow Classes (mock implementations of Android core libraries)

Slide 56

Slide 56 text

Exercise

Slide 57

Slide 57 text

Robolectric InstrumentationTestCase too slow / hard for unit testing? ! vs real device / speedy emulator

Slide 58

Slide 58 text

Robolectric advantages Unit testing fast and easy Make your own shadow objects JUnit 4 supported (Android = JUnit 3)

Slide 59

Slide 59 text

Robolectric disadvantages Does not cover all functionality (sensors, OpenGL, ...)
 → device needed Integration testing (interaction Activities - Services, camera app, ...)

Slide 60

Slide 60 text

Build system

Slide 61

Slide 61 text

Android project setup & build ant gradle

Slide 62

Slide 62 text

Android build system

Slide 63

Slide 63 text

Android Maven plugin https://code.google.com/p/maven-android-plugin/ com.jayway.maven.plugins.android.generation2 android-maven-plugin 3.8.0 !

Slide 64

Slide 64 text

Maven “Software project management and comprehension tool” ! Builds your software and much more ! De-facto standard for Java software builds ! Convention over configuration ! Declarative rather than procedural

Slide 65

Slide 65 text

Maven and Android Android Maven Plugin – core tool ! M2Eclipse, m2e-android – for Eclipse users ! Maven Android SDK Deployer – for SDK libraries ! Maven Android Archetype – project templates

Slide 66

Slide 66 text

Gradle vs Maven Gradle Maven Build variants / product flavors ✔ ✖ Android test framework ✔ ✔ Robotium ✔ ✔ Robolectric ✔ ✔ UIAutomator / Monkey ✖ ✔ Code coverage ✖ ✔ Code quality (pmd, checkstyle, findbugs, lint) ✔ ✔ Aggregation ✔ ✔ Source: https://github.com/stephanenicolas/Quality-Tools-for-Android

Slide 67

Slide 67 text

Mocks Easymock ! Mockito - uses DexMaker to generate mock classes

Slide 68

Slide 68 text

Mocking Mocking Frameworks allow us to test the code you want, without its dependencies. Mock objects can simulate the behaviour of complex objects. Mock objects isolate the unit of code you are testing.

Slide 69

Slide 69 text

Mockito Prepare the mock + behavior ! Test the method of interest ! Validate that the mock saw what we expected / didn’t see anything unexpected

Slide 70

Slide 70 text

Exercise

Slide 71

Slide 71 text

Robotium Extension of the Android testing framework Makes it easier to write UI tests Inherits from ActivityInstrumentationTestCase2 Main class for testing is Solo Solo is initialized with the instrumentation of the testcase and the activity to test https://code.google.com/p/robotium/

Slide 72

Slide 72 text

Exercise

Slide 73

Slide 73 text

Android FEST Syntactic sugar Extension of the FEST library Fluent syntax for checking assertions Makes tests easier to read/write ! https://github.com/square/fest-android

Slide 74

Slide 74 text

Android FEST assertEquals(View.GONE, view.getVisibility()); assertThat(view).isGone(); expected: <8> but was: <4> Expected visibility but was .

Slide 75

Slide 75 text

Android FEST assertEquals(View.GONE, view.getVisibility()); assertThat(view).isGone(); expected: <8> but was: <4> Expected visibility but was . JUNIT ANDROID FEST

Slide 76

Slide 76 text

Android FEST JUNIT ANDROID FEST assertEquals(View.VISIBLE, layout.getVisibility()); assertEquals(VERTICAL, layout.getOrientation()); assertEquals(4, layout.getChildCount()); assertEquals(SHOW_DIVIDERS_MIDDLE, layout.getShowDividers()); assertThat(layout).isVisible() .isVertical() .hasChildCount(4) .hasShowDividers(SHOW_DIVIDERS_MIDDLE);

Slide 77

Slide 77 text

Exercise

Slide 78

Slide 78 text

Spoon Automate test execution across multiple devices ! Aggregate the results ! Aggregation of screenshots http://square.github.io/spoon/

Slide 79

Slide 79 text

Spoon: screenshots Visual inspection of tests execution Spoon.screenshot(activity, "initial_state"); /* Normal test code... */ Spoon.screenshot(activity, "after_login");

Slide 80

Slide 80 text

Exercise

Slide 81

Slide 81 text

Behavior Driven Development

Slide 82

Slide 82 text

(Unit) testing in Android Android SDK (JUnit 3-based) Instrumentation tests ! !

Slide 83

Slide 83 text

Third party libraries Robotium Robolectric Mockito Fest Android Spoon

Slide 84

Slide 84 text

Cloud testing

Slide 85

Slide 85 text

A better Android emulator Automate testing ! Dozens of devices - different screen sizes - different Android versions

Slide 86

Slide 86 text

Manymo

Slide 87

Slide 87 text

Android Gradle integration

Slide 88

Slide 88 text

CI

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

Continuous integration benefits Fast feedback - fewer errors Test everything on every (nightly) build Less manual testing Regression tests without additional effort

Slide 91

Slide 91 text

Android

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

Other

Slide 95

Slide 95 text

Genymotion http://blog.genymobile.com/genymotion-jenkins-android-testing/

Slide 96

Slide 96 text

Repository manager

Slide 97

Slide 97 text

Architecture Simple architecture ! Dependency injection

Slide 98

Slide 98 text

Code quality

Slide 99

Slide 99 text

Measure...

Slide 100

Slide 100 text

Quality Tools for Android https://github.com/stephanenicolas/Quality-Tools- for-Android

Slide 101

Slide 101 text

Quality Tools for Android

Slide 102

Slide 102 text

Build pipeline tools Build (maven - gradle) Dependency repo (nexus - artifactory) Testing framework (JUnit - ...) Test coverage (Cobertura - Emma - Jacoco) Code analysis (Checkstyle, findbugs, pmd, Android Lint) Creation of deployable artifact (buildtool, artifact repo) Trigger next stage

Slide 103

Slide 103 text

Better Android apps

Slide 104

Slide 104 text

References

Slide 105

Slide 105 text

Suggested reading Android Application Testing Guide Diego Torres Milano (9781849513500) Robotium Automated Testing for Android 
 Hrushikesh Zadgaonkar (9781782168010) ! 


Slide 106

Slide 106 text

Suggested reading Test Driven Development: By Example Beck, Kent (978-0321146533) Continuous Integration: Improving 
 Software Quality and Reducing Risk 
 Duvall, Paul M. et al. (978-0321336385) Working Effectively with Legacy Code
 Feathers, Michael (978-0131177055) 


Slide 107

Slide 107 text

Suggested viewing Google IO sessions

Slide 108

Slide 108 text

Q + A

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

Contact information iDA MediaFoundry
 [email protected]
 @iDAMediaFoundry ! Filip
 [email protected]
 @fmaelbrancke ! Peter
 [email protected]
 @peterkuterna