Version First Edition
Platform iOS 13
Language Swift 5.1
Editor Xcode 11

Learn How to Test iOS Applications!

This book is for intermediate iOS developers who already know the basics of iOS and Swift development but want to learn how to write code which is both testable and maintainable.

To start, you’ll learn the TDD Cycle and how to implement these concepts within an iOS application. The book then takes you through Test Expressions and Expectation so that you can test synchronous code. You’ll then write tests to verify networking endpoints and the ability to mock the returned results, followed by writing tests that run against authentication endpoints. Continue trouble-shooting your apps by understanding common legacy problems, as well as breaking dependencies into modules. And, finally, refactor large classes into smaller, more manageable classes and objects.

Before You Begin

This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more.

i

iii

Section I: Hello, TDD!

This section is a high-level introduction to test-driven development, how it works and why you should use it. You’ll also learn about the TDD cycle in this chapter, and you’ll use this throughout the rest of the book.

Test-driven development, or TDD, is an iterative way to develop software by iteratively making many small changes backed by tests.

1

In the previous chapter, you learned that test-driven development boils down to a simple process called the TDD Cycle. It has four steps and is also called the Red-Green-Refactor Cycle.

2

Section II: Beginning TDD

This section will teach you the basics of test-driven development (TDD). You’ll learn about setting up your app for TDD, test expressions, dependency injection, mocks and test expectations.

Along the way, you’ll build a fitness app to learn the basics of TDD through hands-on practice.

The goal of this chapter is to give you a feel for how Xcode testing works by creating a test target with a few tests. You'll do this while learning the key concepts of TDD.

3

This chapter covers how to use the XCTAssert functions. These are the primary actors of the test infrastructure. Next, you'll learn how to use the host application to drive view controller unit testing. Then, you'll go through gathering code coverage to verify the minimum amount of testing. Finally, you'll use the test debugger to find and fix test errors.

4

In the previous chapters you built out the app's state based upon what the user can do with the Start button. The main part of the app relies on responding to changes as the user moves around and records steps. These actions create events outside the program's control. XCTestExpectation is the tool for testing things that happen outside the direct flow.

5

In this chapter you'll learn how to use mocks to test code that depends on system or external services without needing to call services: They may not be available, usable or reliable. These techniques allow you to test error conditions like a failed save and to isolate logic from SDKs like CoreMotion and HealthKit.

6

Section III: TDD with Networking

This section will teach you test-driven development with networking.

You’ll get hands-on experience creating a puppy-buying app that interacts with a backend service. You’ll learn how to do TDD for RESTful networking, using network clients and downloading images throughout this section.

You'll complete a puppy-adoption app called Dog Patch throughout this section. This app connects dog lovers with kind, professional breeders to find the puppy of their dreams. A prospective owner first browses available puppy listings within the app.

7

This chapter will introduce how to do TDD for RESTful networking (i.e. not using WebSockets).

8

You'll use the previously created network client in your application by updating the ListingsViewController to use the DogPatchClient to actually network.

9

You'll use TDD to create an ImageClient for handling images. You can use that ImageClient anywhere you need it in the app.

10

Section IV: TDD in Legacy Apps

This section will show you how to start test-driven development in a legacy app that lacks sufficient unit tests. You’ll learn strategies for introducing TDD into existing apps, methods for visualizing and splitting up dependencies, ways to add features safely alongside existing code and how to refactor large classes.

Throughout this section, you’ll introduce TDD into an app for managing a business. The app is feature-rich with spaghetti code and ready for a TDD clean up!

Several techniques and concepts in this section were inspired by Michael Feather’s book Working Effectively with Legacy Code. Reading that book isn’t a strict requirement for working through these chapters. However, you’ll likely benefit by having some familiarity with the topics herein if you already have read it!

Beginning TDD on an existing, “legacy” project is much different than starting TDD on a new project. Often times, the original team has long left, and the code base is not fully understood. The project has few if any unit tests, lacks documentation and is slow to build.

11

Before you can start making changes, you first need to understand how a system works and which classes relate to one another. This chapter will teach you about dependency maps.

12

It’s always safer to make a change when you have tests in place already. In the absence of existing tests, however, you may need to make changes just to be able to add tests! One of the most common reasons for this is tightly-coupled dependencies: you can’t add tests to a class because it depends on other classes that depend on other classes… View controllers especially are often victims to this issue.

13

You’ll continue the work from the last chapter, further breaking **MyBiz** into modules so you can reuse the login functionality. You’ll learn how to define clean boundaries in the code to create logical units. Through the use of tests, you’ll make sure the new architecture works and the app continues to function.

14

You won’t always have the time, or it may simply not be feasible, to break dependencies of a very large class. In this chapter, you’ll learn strategies to add functionality to an existing class while at the same time avoiding modifying it! You’ll learn two main strategies to do this: Sprouts and Decorators.

15

Meet the team

Contributors

Who is this book for

This book is for intermediate iOS developers who already know the basics of iOS and Swift development but want to learn how to write code which is both testable and maintainable.

Concepts covered in this book

  • The TDD Cycle
  • Test Expressions and Expectations
  • Test RESTful Networking
  • Test Authentication
  • Legacy Problems
  • Breaking Dependencies into Modules
  • Refactoring Large Classes

Version history

First Edition · iOS 13, Swift 5.1, Xcode 11

v1.0 · Oct 2 2019 · Michael Katz & Joshua Greene
Pro

iOS Test-Driven Development by Tutorials

By Michael Katz & Joshua Greene

The book that teaches you to write maintainable and sustainable apps by building them with testing in mind or adding tests to already-written apps.

Read for Free with an Ultimate Pro Subscription* * Includes this and all other books in our online library See all benefits
Buy Individually $59.99* *Includes access to all of our online reading features.
See buying options
Hide buying options

All videos. All books.
One low price.

A raywenderlich.com subscription is the best way to learn and master mobile development — plans start at just $19.99/month!

Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.