Reader small image

You're reading from  Test-Driven iOS Development with Swift - Fourth Edition

Product typeBook
Published inApr 2022
PublisherPackt
ISBN-139781803232485
Edition4th Edition
Right arrow
Author (1)
Dr. Dominik Hauser
Dr. Dominik Hauser
author image
Dr. Dominik Hauser

Dr. Dominik Hauser is an iOS developer working for a small company in western Germany. In over 11 years as an iOS developer, he has worked on many different apps, both large and small. In his spare time, Dominik builds small (often silly) apps and explores how he can become a better iOS developer. He talks at conferences, writes books, and is active in the iOS community in general. His most successful open source component (in terms of stars) is a pull-to-refresh control for a table view that consists of an actual SpriteKit game. Before Dominik became an iOS developer, he was a physicist, researching the most powerful photon sources within our galaxy.
Read more about Dr. Dominik Hauser

Right arrow

Preface

Automatic tests are an essential part of agile software development, especially unit tests, with their quick and reliable feedback helping developers to keep projects maintainable and clean. Test-driven development (TDD) methodology gives developers clear rules about how to build scalable, maintainable, and – as such – agile projects. Often, once the initial aversion to writing tests before the code is overcome, developers stick to TDD because they feel that their code is better and they are more confident in the result.

In this book, we will explore how to write unit tests for many different aspects of iOS development for iOS 15+. All that will be done in the context of test-driven development. The book starts with an introduction to testing in general and TDD in iOS apps and continues with building a complete app throughout the book. It covers basic UI structures, the Combine framework, async/await, and even SwiftUI.

After you've worked through the book and challenged yourself in the exercises, you will be able to write tests for a variety of code in iOS development, and you will have the tools to decide what to learn next in your journey to become an expert in test-driven iOS development.

Who this book is for

TDD is a proven way to find software bugs early. Writing tests before you code improves the structure and maintainability of your apps. This book will guide you through the steps for creating a complete app using TDD and cover the core elements of iOS apps: view controllers, views, navigation, networking, Combine, and SwiftUI.

If you have already made your first small iOS apps and want to learn how to improve your work using automated unit tests, then this book is for you.

What this book covers

Chapter 1, Your First Unit Tests, sees the first unit tests at work. We write real tests for a fictional blogging app and explore the different kinds of assertions in XCTest, a testing framework from Apple.

Chapter 2, Understanding Test-Driven Development, looks at test-driven development and how it can help us developers to write maintainable code.

Chapter 3, Test-Driven Development in Xcode, brings the insights from the first two chapters together and looks at how test-driven development works in Xcode. You'll learn some tricks and configurations that make Xcode a valuable testing tool.

Chapter 4, The App We Are Going To Build, discusses the app we are going to build in the rest of the book. This chapter ends with setting up the project for the app in Xcode.

Chapter 5, Building a Structure for ToDo Items, shows how to build the model layer of our app. By working on it, you will learn how to write tests for Combine code.

Chapter 6, Testing, Loading, and Saving Data, addresses the fact that the data used in the app needs to be saved and loaded in the file system of the iOS device. In this chapter, we build the class that is responsible for this task.

Chapter 7, Building a Table View Controller for the To-Do Items, shows how to write tests for a table view with a diffable data source. You will learn how to test updates of table view cells and the selection of cells.

Chapter 8, Building a Simple Detail View, explores how to test user interface elements such as labels, buttons, and maps. We also take a look at how to test actions of the user that change the data in the model layer.

Chapter 9, Test-Driven Input View in SwiftUI, shows how to build and test a view created using SwiftUI. In order to be able to test SwiftUI code, we add a third-party testing library to the testing target.

Chapter 10, Testing Networking Code, looks at writing tests for the new async/await APIs of URLSession. This will allow you to write clean tests that simulate the network communication with a fast mock object.

Chapter 11, Easy Navigation with Coordinators, the final chapter, shows how to write tests for navigation between the view controllers of our app. This allows us, finally, to see our small app running on the simulator. We fix the last bugs using TDD and end up with a working app.

To get the most out of this book

You need the latest version of Xcode installed on your Mac. The code in this book has been tested with Xcode 13 and Swift 5.5 but it should also work with newer versions of Xcode and Swift.

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book's GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

You should try to do all the exercises in the book. They are designed to give you more insight and strengthen your experience.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Test-Driven-iOS-Development-with-Swift-Fourth-Edition. If there's an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots and diagrams used in this book. You can download it here: http:https://static.packt-cdn.com/downloads/9781803232485_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "A table view is usually represented by UITableViewController, which is also the data source and delegate for the table view."

A block of code is set as follows:

// APIClient.swift
lazy var geoCoder: GeoCoderProtocol
  = CLGeocoder()

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

mkdir src/client/apollo touch src/client/apollo/index.js

Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: "From the Details screen, the user will be able to check an item."

Tips or Important Notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, email us at customercare@packtpub.com and mention the book title in the subject of your message.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Share Your Thoughts

Once you've read Test-Driven iOS Development with Swift Fourth Edition, we'd love to hear your thoughts! Please click here to go straight to the Amazon review pagefor this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we're delivering excellent quality content.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Test-Driven iOS Development with Swift - Fourth Edition
Published in: Apr 2022Publisher: PacktISBN-13: 9781803232485
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Dr. Dominik Hauser

Dr. Dominik Hauser is an iOS developer working for a small company in western Germany. In over 11 years as an iOS developer, he has worked on many different apps, both large and small. In his spare time, Dominik builds small (often silly) apps and explores how he can become a better iOS developer. He talks at conferences, writes books, and is active in the iOS community in general. His most successful open source component (in terms of stars) is a pull-to-refresh control for a table view that consists of an actual SpriteKit game. Before Dominik became an iOS developer, he was a physicist, researching the most powerful photon sources within our galaxy.
Read more about Dr. Dominik Hauser