
C om m un ity E x p er ien c e D ist il l e d Android Application Testing Guide Build intensively tested and bug free Android applications Diego Torres Milano PACKT] www.allitebooks.com
www.allitebooks.com

Android Application Testing Guide Build intensively tested and bug free Android applications Diego Torres Milano [PACKT] PUBLISHING BIRMINGHAM-MUMBAI www.allitebooks.com
Android Application Testing Guide Build intensively tested and bug free Android applications Diego Torres Milano BIRMINGHAM - MUMBAI www.allitebooks.com

Android Application Testing Guide Copyright2011 Packt Publishing All rights reserved.No part of this book may be reproduced,stored in a retrieval critical articles or reviews. Every effort has been made in the preparation of this book to ensure th accuracy old e m on presen e tied.N a m t held liable fo caused or alleged to be caused directly or indirectly by this book. damage n about all of the oducts mentioned in this bo iate use of capitals. mpbthepreof cpils First published:June 2011 Production Reference:1170611 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham,B27 6PA,UK ISBN978-1-849513-50-0 www.packtpub.com Cover Image by Asher Wishkerman(a.wishkermanampic.de) www.allitebooks.com
Android Application Testing Guide Copyright © 2011 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: June 2011 Production Reference: 1170611 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849513-50-0 www.packtpub.com Cover Image by Asher Wishkerman (a.wishkerman@mpic.de ) www.allitebooks.com

Credits Project Coordinator Joel Goveya Reviewers Proofreaders Paul Bourdeaux Aaron Nash Noah Eltzroth Stephen Silk Tomas Malmsten Indexer Gabor Paller Hemangini Bari Abhinav Tyagi Graphics Acquisition Editor Nilesh Mohite Tarun Singh Production Coordinator Kruthika Bangera Technical Editor Conrad Sardinha www.allitebooks.com
Credits Author Diego Torres Milano Reviewers Paul Bourdeaux Noah Eltzroth Tomas Malmsten Gabor Paller Abhinav Tyagi Acquisition Editor Tarun Singh Development Editor Chris Rodrigues Technical Editor Conrad Sardinha Project Coordinator Joel Goveya Proofreaders Aaron Nash Stephen Silk Indexer Hemangini Bari Graphics Nilesh Mohite Production Coordinator Kruthika Bangera Cover Work Kruthika Bangera www.allitebooks.com

About the Author Diego Torres Milano has been involved with the Android platform since its inception,at the end of 2007,when he started exploring and researching the platform rnt andanand This is reflected by a number of articles mainly published in his personal blog bile Dev Ca (Tokyo), anies in Europe also a A various comp ope essful PXES Inis piect (that was late ad s2coPXES is a Linux-based oneratin ecialized for thin clients used by hundreds of thousands of thin clients all over the world.This in Europe decided to us ecause cy;orge png co to a1 te t cial nes,to every one e pr ojects that he has founded we d he has contributing t various Linux distributions such as RedHat,Fedora,and Ubuntu. esentations in Linux World,LinuxTag,GUADEC ES University of Buer s and soon He has been developin in One n Source projects,and advising companie He can be contacted at dtmilano@gmail.com www.allitebooks.com
About the Author Diego Torres Milano has been involved with the Android platform since its inception, at the end of 2007, when he started exploring and researching the platform possibilities, mainly in the areas of User Interfaces, Unit and Acceptance Tests, and Test Driven Development. This is reflected by a number of articles mainly published in his personal blog (http://dtmilano.blogspot.com) and his participation as a lecturer in some conferences and courses like Mobile Dev Camp 2008 in Amsterdam (Netherlands) and Japan Linux Symposium 2009 (Tokyo), Droidcon London 2009, Skillsmatter 2009 (London, UK), and he has also authored Android training courses delivered to various companies in Europe. Previously, he was the founder and developer of several Open Source projects, mainly CULT Universal Linux Thin Project (cult-thinclient.sf.net) and the very successful PXES Universal Linux Thin Client project (that was later acquired by 2X Software, www.2x.com). PXES is a Linux-based Operating System specialized for thin clients used by hundreds of thousands of thin clients all over the world. This project has a popularity peak of 35M hits and 400K downloads from SourceForge in 2005. This project had a dual impact: big companies in Europe decided to use it because of improved security and efficiency; organizations, institutions, and schools in some developing countries in South America, Africa, and Asia decided to use it because of the minimal hardware requirements to have a huge social impact providing computers, sometimes recycled ones, to everyone. Among the other Open Source projects that he has founded we can mention Autoglade, Gnome-tla, JGlade, and he has been contributing to various Linux distributions such as RedHat, Fedora, and Ubuntu. He also has been giving presentations in Linux World, LinuxTag, GUADEC ES, University of Buenos Aires, and so on. He has been developing software, participating in Open Source projects, and advising companies worldwide for more than 15 years. He can be contacted at dtmilano@gmail.com. www.allitebooks.com

Firstly,I would like to thank my family:Laura,Augusto and Octavio for their patience and consideration.The time I borrowed to achieve this goal was mostly theirs. onal friend and in3 azing adventure more than 15 ve pioneered the provision of Linux services and su 82npm in South America.He certainly taught me to be a bit more consultant and a bit less hacker(but I'm a very bad student:))And special thanks to Ricston's Peter Delia with whom we started providing 200 reality dictated by the market. And lastly I would like to thank all the reviewers and the Packt Publishing team who gave me their opinior n,suggestions,and corrections on early manuscripts;wit out them the book would never have had the quality it endowed www.allitebooks.com
Firstly, I would like to thank my family: Laura, Augusto and Octavio for their patience and consideration. The time I borrowed to achieve this goal was mostly theirs. Secondly I would like to thank my personal friend and IN3 Integracion Informatica co-founder, Caludio Palonsky, with whom we started this amazing adventure more than 15 years ago when we pioneered the provision of Linux services and support to enterprises in South America. He certainly taught me to be a bit more consultant and a bit less hacker (but I'm a very bad student :-)). And special thanks to Ricston's Peter Delia with whom we started providing Android training services throughout Europe as early as mid 2008 when Android was just a beautiful dream of having a mainstream Open Source operating system in the mobile arena. This is now a reality dictated by the market. And lastly I would like to thank all the reviewers and the Packt Publishing team who gave me their opinion, suggestions, and corrections on early manuscripts; without them the book would never have had the quality it endowed. www.allitebooks.com

About the Reviewers Paul Bourdeaux is the Senior Software Engineer and Application Development Team Lead at Sundog,a marketing and technology company based in the Midwest. He has a strong background in traditional software engineering,has authored several white ul is the a Dase Noah Eltzroth teaches dynamic web development at the Sullivan College of Technology and Design in Louisville,Kentucky.He enjoys working on a variety of 、Aa中pih出心hnysp8品haan& Tomas Malmsten has been working with software development for over a decade.During this time he has had the opportunity to work with a vast variety of technologies in various different business settings.His main focus has been the Java as worked with everything from large enterprise systems to Tomas is a passionate software craftsman who strives for exceller nFrom customer service and interaction to well craftent You can get in touch with Tomas through any of the following means Blog:http://www.tomasmalmsten.com .Twitter:http://twitter.com/tomasmalmster E-mail:publicetomasmalmsten.com LinkedIn:http://se.linkedin.com/in/tomasmalmster www.allitebooks.com
About the Reviewers Paul Bourdeaux is the Senior Software Engineer and Application Development Team Lead at Sundog, a marketing and technology company based in the Midwest. He has a strong background in traditional software engineering, has authored several white papers relating to mobile marketing and software engineering, and has presented at both national and regional software engineering conferences. Paul is the mobile marketing expert at Sundog, and his passion lies in mobile and cloud based software engineering. Noah Eltzroth teaches dynamic web development at the Sullivan College of Technology and Design in Louisville, Kentucky. He enjoys working on a variety of different software projects including business-oriented portals, data processing, and Android applications. In his free time, Noah enjoys programming in both Java and PHP. Tomas Malmsten has been working with software development for over a decade. During this time he has had the opportunity to work with a vast variety of technologies in various different business settings. His main focus has been the Java ecosystem where he has worked with everything from large enterprise systems to Android application development. Tomas is a passionate software craftsman who strives for excellence in all aspects of programming. From customer service and interaction to well crafted maintainable programs. You can get in touch with Tomas through any of the following means: • Blog: http://www.tomasmalmsten.com • Twitter: http://twitter.com/tomasmalmsten • E-mail: public@tomasmalmsten.com • LinkedIn: http://se.linkedin.com/in/tomasmalmsten www.allitebooks.com

ees from the technical universit Gabor Paller receivediNd lapest in 1992 and 1996.r Positionn Nokia and Nok Research Center.Hisintersts included wireles After having left kia,he worked at On -mobile con s,and the e ne works on IMS ks popular My ife reviewed a n ber Abhinav Tyagi is a Computer Science graduate from Pune.He also holds a post graduate diploma in Advanced Computing from CDAC,Pune.He developed several applications while working as a Software Engineer at Antarix Networks, e to thank Joel Goveya and Tarun Singh for giving me this www.allitebooks.com
Gábor Paller received his MSc. and PhD. degrees from the Technical University of Budapest in 1992 and 1996, respectively. Dr. Paller joined Nokia in 1998 and held positions in Nokia R&D and Nokia Research Center. His interests included wireless protocol development, mobile device management, mobile Java and middleware. He was also involved in standardization and joint research program activities. After having left Nokia, he worked at OnRelay Ltd. on fixed-mobile convergence technologies, and then in 2010 he joined Ericsson where he works on IMS. Gabor Paller runs the popular My Life with Android blog and reviewed a number of Android books. Abhinav Tyagi is a Computer Science graduate from Pune. He also holds a post graduate diploma in Advanced Computing from CDAC, Pune. He developed several Android applications while working as a Software Engineer at Antarix Networks, Mumbai. He is currently working on telecom protocols as a Research & Development Engineer at Nokia Siemens Networks. I would like to thank Joel Goveya and Tarun Singh for giving me this wonderful opportunity. www.allitebooks.com

www.PacktPub.com Support files,eBooks,discount offers and more You might want to visit www.Packtpub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published,with PDF and ePub files available?You can upgrade to the eBook version at www.PacktPub. com and as a print book customer,you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com,you can also read a collection of free technical articles,sign 思kcamaecokeawstesandoeaneowethsnedoumsandaiasomh PACKTLiB http://PacktLib.PacktPub.com PacktLib s Pack's nline digital books Why Subscribe? Fully searchable across every book published by Packt .Copy and paste,print and bookmark content On demand and accessible via web browser Free Access for Packt account holders for immediate access. www.allitebooks.com
www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why Subscribe? • Fully searchable across every book published by Packt • Copy and paste, print and bookmark content • On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. www.allitebooks.com

Table of Contents Preface Chapter 1:Getting Started with Testing Brief history Software bugs 8 How bugs severely affect your projects Why,what,how,and when to test nat to test The test fixture hod t 9122233555660022 Performance tests System tests 3 Android testing framework Instrumentation Test targets Summary 3232526 Chapter 2:Testing on Android JUnit Creating the Android main project Creating the Android test project 29 www.allitebooks.com
Table of Contents Preface 1 Chapter 1: Getting Started with Testing 7 Brief history 7 Software bugs 8 How bugs severely affect your projects 9 Why, what, how, and when to test 9 What to test 11 Activity lifecycle events 12 Database and filesystem operations 12 Physical characteristics of the device 12 Types of tests 13 Unit tests 13 The test fixture 15 The setUp() method 15 The tearDown() method 15 Test preconditions 16 The actual tests 16 Integration tests 20 Functional or acceptance tests 20 Test case scenario 22 Performance tests 22 System tests 23 Android testing framework 23 Instrumentation 23 Test targets 25 Summary 26 Chapter 2: Testing on Android 27 JUnit 28 Creating the Android main project 28 Creating the Android test project 29 www.allitebooks.com