Test-Driven iOS Development with Swift 3 Write testable and maintainable code to develop highly-functional iOS apps Dr. Dominik Hauser BIRMINGHAM - MUMBAI Test-Driven iOS Development with Swift 3 Copyright © 2016 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: February 2015 Second edition: October 2016 Production reference: 1251016 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78712-907-8 www.packtpub.com Credits Author Copy Editor Dr. Dominik Hauser Yesha Gangani Reviewer Project Coordinator S Ravi Shankar Sheejal Shah Commissioning Editor Proofreader Ashwin Nair Safis Editing Acquisition Editor Indexer Reshma Raman Rekha Nair Content Development Editor Production Coordinator Parshva Sheth Aparna Bhagat Technical Editor Rutuja Vaze About the Author Dr. Dominik Hauser completed his Ph.D. from the University of Heidelberg in Physics. While working as a university professor, he started iOS development in his spare time. His first app on Physics has been an astounding success worldwide. Since then, he turned himself into a full-time iOS developer, crediting number of successful apps to his name. He has been a Swift developer since day one and runs a blog on iOS development, www.swiftandpainless.com. I would like to thank my wife, Isa. Thank you for all your support during all those years and especially the last months while I wrote this book. I also want to thank all those great people in our community. I learned so much by reading excellent blog posts. I proud to be part of it. About the Reviewer S Ravi Shankar is a multi-skilled software consultant with over 17+ years of experience in IT Industry. Good all-around ability to work in different technologies and extensive experience in product development, system maintenance and support. A polyglot and a self-taught programmer with hands on experience in Swift, Objective-C and Java. It was a pleasure to work with Sheejal and thanks to Packt publishing for giving me this opportunity. www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. 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 [email protected] 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. https://www.packtpub.com/mapt Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser Table of Contents Preface 1 Chapter 1: Your First Unit Tests 6 Building your first automatic unit test 7 What are unit tests? 7 Implementing a unit test example 7 Important built-in assert functions 12 Understanding TDD 15 The TDD workflow – red, green, and refactor 16 Red 16 Green 17 Refactor 17 TDD in Xcode 17 An example of TDD 18 Red – example 1 19 Green – example 1 20 Refactor – example 1 20 Red – example 2 21 Green – example 2 21 Refactor – example 2 22 A recap 24 Finding information about tests in Xcode 24 Test Navigator 24 Tests overview 25 Running tests 27 Running one specific test 27 Running all tests in a test case 28 Running a group of tests 28 The setUp() and tearDown() methods 29 Debugging tests 30 Breakpoint that breaks on test failure 31 The test again feature 32 The advantages of TDD 32 The disadvantages of TDD 33 What to test 34 Summary 35 Chapter 2: Planning and Structuring Your Test-Driven iOS App 36 The task list view 37 The task detail view 39 The task input view 40 The structure of the app 41 The Table View Controller, the delegate, and the data source 41 Table view cells 42 The model 42 Other View Controllers 42 The development strategy 43 Getting started with Xcode 43 Setting up useful Xcode behaviors for testing 44 Useful build behaviors 45 Testing behaviors 45 Summary 47 Chapter 3: A Test-Driven Data Model 48 Implementing the ToDoItem struct 49 Adding a title property 49 Adding an itemDescription property 51 Removing a hidden source of bugs 52 Adding a timestamp property 54 Adding a location property 55 Implementing the Location struct 56 Adding a coordinate property 57 Implementing the ItemManager class 58 Count 58 Adding and checking items 61 Equatable 67 Removing all items 76 Ensuring uniqueness 77 Summary 78 Chapter 4: A Test-Driven View Controller 79 Implementing ItemListViewController 80 Implementing ItemListDataProvider 90 Conducting the first tests 90 Fake objects 96 Using mocks 97 Checking and unchecking items 108 Implementing ItemCell 110 Implementing DetailViewController 117 [ ii ] Implementing InputViewController 122 Summary 128 Chapter 5: Testing Network Code 129 Implementing tests using a web service 130 Implementing a login request 133 Handling errors 141 Summary 145 Chapter 6: Putting It All Together 146 Connecting parts 147 The initial View Controller 147 Showing the input view 148 Showing the detail view 156 Serialization and deserialization 160 Functional tests 168 Adding a UI test target 169 Recording and testing 169 Summary 175 Chapter 7: Code Coverage and Continuous Integration 176 Enabling code coverage 176 Code coverage in Xcode 177 What code coverage is enough? 180 Continuous integration 180 Installing and setting up Xcode Server 181 Adding bots 186 Automatic deployment with fastlane 190 Installing fastlane 191 Setting up 191 Summary 192 Chapter 8: Where to Go from Here 193 What we have learned so far 193 Integration tests 194 UI tests 194 Behavior-Driven Development 195 TDD in existing projects 196 More information about TDD 197 Summary 197 Index 198 [ iii ]