Test-Driven iOS Development with Swift - Fourth Edition

Book description

Build robust applications using TDD with Swift 5.5 and become a TDD expert by writing tests for view controller, views, network code, and even SwiftUI view

Key Features

  • Build a complete iOS app using test-driven development
  • Explore testing view controllers, table views, navigation, and network code
  • Learn how to write tests for Combine and SwiftUI code

Book Description

Test-driven development (TDD) is a proven way to find software bugs earlier on in software development. Writing tests before you code improves the structure and maintainability of your apps, and so using TDD in combination with Swift 5.5's improved syntax leaves you with no excuse for writing bad code.

Developers working with iOS will be able to put their knowledge to work with this practical guide to TDD in iOS. This book will help you grasp the fundamentals and show you how to run TDD with Xcode. You'll learn how to test network code, navigate between different parts of the app, run asynchronous tests, and much more. Using practical, real-world examples, you'll begin with an overview of the TDD workflow and get to grips with unit testing concepts and code cycles. You'll then develop an entire iOS app using TDD while exploring different strategies for writing tests for models, view controllers, and networking code. Additionally, you'll explore how to test the user interface and business logic of iOS apps and even write tests for the network layer of the sample app.

By the end of this TDD book, you'll be able to implement TDD methodologies comfortably in your day-to-day development for building scalable and robust applications.

What you will learn

  • Implement TDD in Swift application development
  • Detect bugs before you run code using the TDD approach
  • Use TDD to build models, view controllers, and views
  • Test network code with asynchronous tests and stubs
  • Write code that's a joy to read and maintain
  • Design functional tests to suit your software requirements
  • Discover scenarios where TDD should be applied and avoided

Who this book is for

This book is for iOS developers looking to apply TDD to build maintainable and scalable applications. Intermediate-level developers with Swift application development experience will be able to make the most out of this book. Prior experience of applying TDD to Swift applications is not required.

Table of contents

  1. Fourth Edition
  2. Test-Driven iOS Development with Swift
  3. Fourth Edition
  4. Contributors
  5. About the author
  6. About the reviewers
  7. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  8. Section 1 –The Basics of Test-Driven iOS Development
  9. Chapter 1: Your First Unit Tests
    1. Technical requirements
    2. Building your first automatic unit test
      1. What are unit tests?
      2. Implementing a unit test example
      3. Disabling slow UI tests
    3. Assert functions in the XCTest framework
      1. Custom assert functions
    4. Understanding the difference from other kinds of tests
      1. Integration tests
      2. UI tests
      3. Snapshot tests
      4. Manual tests
    5. Summary
    6. Exercises
  10. Chapter 2: Understanding Test-Driven Development
    1. The origin of TDD
    2. The TDD workflow – red, green, refactor
      1. Red
      2. Green
      3. Refactor
    3. The advantages of TDD
    4. The disadvantages of TDD
    5. What to test
    6. Summary
  11. Chapter 3: Test-Driven Development in Xcode
    1. Technical requirements
    2. An example of TDD
      1. Capitalize headline – red
      2. Capitalize headline – green
      3. Capitalize headline – refactor
      4. Capitalize headline 2 – red
      5. Capitalize headline 2 – green
      6. Capitalize headline 2 – refactor
      7. A recap
    3. Finding information about tests in Xcode
      1. Test navigator
      2. Test overview
    4. Running tests
      1. Running one specific test
      2. Running all tests in a test case
      3. Running a group of tests
    5. Setting up and tearing down
    6. Debugging tests
      1. A breakpoint that breaks on test failure
      2. The test again feature
    7. Summary
    8. Exercises
  12. Section 2 –The Data Model
  13. Chapter 4: The App We Are Going to Build
    1. Technical requirements
    2. A list of to-do items
    3. A view for the details of a to-do item
    4. A view to add to-do items
    5. The structure of the app
      1. The table view controller, the delegate, and the data source
      2. Table view cells
      3. The model
      4. Other views
      5. The development strategy
    6. Getting started in Xcode
      1. Setting up useful Xcode behaviors for testing
      2. Useful build behaviors
      3. Testing behaviors
    7. Summary
    8. Exercises
  14. Chapter 5: Building a Structure for ToDo Items
    1. Technical requirements
    2. Implementing the ToDoItem struct
      1. Adding a title property
      2. Adding an itemDescription property
      3. Removing a hidden source of bugs
      4. Adding a timestamp property
      5. Dealing with optional values in unit tests
      6. Adding a location property
    3. Implementing the Location struct
      1. Adding a coordinate property
    4. Summary
    5. Exercises
  15. Chapter 6: Testing, Loading, and Saving Data
    1. Technical requirements
    2. Publishing changes with Combine
      1. Testing asynchronous Combine code
      2. Making ToDoItem equatable
    3. Checking items
    4. Storing and loading ToDoItems
      1. Implementing storing and loading
      2. Cleaning up the code
    5. Summary
    6. Exercises
  16. Section 3 –Views and View Controllers
  17. Chapter 7: Building a Table View Controller for the To-Do Items
    1. Technical requirements
    2. Adding the table view for the to-do items
    3. Testing the data source of a table view
      1. Adding a test double
      2. Using test doubles to implement a number of rows
      3. Using test doubles to implement setting up the to-do item cell
    4. Refactoring to a diffable data source
    5. Presenting two sections
    6. Implementing the delegate of a table view
    7. Summary
    8. Exercises
  18. Chapter 8: Building a Simple Detail View
    1. Technical requirements
    2. Adding labels, a button, and a map
    3. Filling in the data
    4. Checking the to-do item
    5. Summary
    6. Exercises
  19. Chapter 9: Test-Driven Input View in SwiftUI
    1. Technical requirement
    2. Adding the ViewInspector package
    3. Using ViewInspector to test a simple view
      1. Adding a title text field
      2. Adding a DatePicker
      3. Improving the test code and the implementation
      4. Adding another text field
      5. Improving the user interface
      6. Adding an address text field and a button
    4. Testing button actions with ViewInspector
    5. Summary
    6. Exercises
  20. Section 4 –Networking and Navigation
  21. Chapter 10: Testing Networking Code
    1. Mocking CLGeoCoder
      1. Cleaning your project
      2. Preparations for the tests
      3. Creating the first test
    2. Testing async/await code that communicates with URLSession
    3. Handling errors
    4. Summary
    5. Exercises
  22. Chapter 11: Easy Navigation with Coordinators
    1. Technical requirement
    2. Testing the app's setup
    3. Navigating to the details
    4. Navigating to a modal view
    5. Adding missing parts
      1. Making the cells visible
      2. Dismissing the input view
      3. Making the due date visible in the details
      4. Fixing the wrong item being selected
      5. Fixing the layout of the table view
    6. Summary
    7. Exercises
    8. Why subscribe?
  23. Other Books You May Enjoy
    1. Packt is searching for authors like you

Product information

  • Title: Test-Driven iOS Development with Swift - Fourth Edition
  • Author(s): Dr. Dominik Hauser
  • Release date: April 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803232485