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 2016
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 Dr. Dominik Hauser | Copy Editor Yesha Gangani |
Reviewer S Ravi Shankar | Project Coordinator Sheejal Shah |
Commissioning Editor Ashwin Nair | Proofreader Safis Editing |
Acquisition Editor Reshma Raman | Indexer Rekha Nair |
Content Development Editor Parshva Sheth | Production Coordinator 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 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.
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
Preface
iOS projects have become bigger and more complex. Many projects have already surpassed desktop applications in their complexity. One important strategy to manage this complexity is through the use of unit tests. By writing tests, a developer can point out the intention of the code and provide a safety net against the introduction of bugs.
By writing the tests first (Test-Driven Development), the developer focuses on the problem. This way, they are forced to think about the domain and rephrase a feature request using their own understanding by writing the test. In addition to this, applications written using Test-Driven Development (TDD) only contain code that is needed to solve the problem.
As a result, the code is clearer, and the developer gains more confidence that the code actually works.
In this book, you will develop an entire iOS app using TDD. You will experience different strategies of writing tests for models, View Controller, and networking code.
What this book covers
, Your First Unit Tests , walks you through your first unit tests using Xcode and discusses the benefits of of using TDD.
, Planning and Structuring Your Test-Driven iOS App , introduces the app you are going to write through the course of this book and how to set up a project in Xcode.
, A Test-Driven Data Model , discusses the TDD of a data model.
, A Test-Driven View Controller , shows you how to write tests for View Controller, and describes how to use fake objects to isolate micro features for the test.
, Testing Network Code , teaches you to test network code using stubs to fake a server component before it is developed.
, Putting It All Together , walks you through the integration of all the different parts developed in previous chapters and shows the use of functional tests.
, Code Coverage and Continuous Integration , shows you how to measure the code coverage of your tests using Xcode and introduces you to continuous integration.
, Where to Go from Here , wraps up and shows you the possible next steps to improve your acquired testing skills.
What you need for this book
The following hardware and software is needed to follow the code examples inthe book:
- Mac with Sierra (MAC OS 10.12)
- Xcode 8
Who this book is for
If debugging iOS apps is a nerve-racking task for you and you are looking for a fix, this book is for you.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "To be able to write tests for your code, you need to import the module with the