small medium large xlarge

iOS Unit Testing by Example: XCTest Tips and Techniques Using Swift

by

Cover image for iOS Unit Testing by Example

iOS Unit Testing by Example

XCTest Tips and Techniques Using Swift

by

Fearlessly change the design of your iOS code with solid unit tests. Use Xcode’s built-in test framework XCTest and Swift to get rapid feedback on all your code — including legacy code. Learn the tricks and techniques of testing all iOS code, especially view controllers (UIViewControllers), which are critical to iOS apps. Learn to isolate and replace dependencies in legacy code written without tests. Practice safe refactoring that makes these tests possible, and watch all your changes get verified quickly and automatically. Make even the boldest code changes with complete confidence.

Choose Your Format(s)

  • $25.95 In Stock
    • Beta: What do I get?

  • Ebooks are DRM free.

  • Ebook delivery options.

  • The Paper Book will ship on 2019-11-10 (roughly).

About this Title

Skill-meter-3-6
Pages: 300 (est)
Published: 2019-11-10
Release: B3.0 (2019-07-08)
ISBN: 978-1-68050-681-5

Manual code and UI testing get slower the deeper your navigation hierarchy goes. It can take several taps just to reach a particular screen, never mind the actual workflow tests. Automatic unit testing offers such rapid feedback that it can change the rules of development. Bring testing to iOS development, even for legacy code. Use XCTest to write unit tests in Swift for all your code.

iOS developers typically reserve unit tests for their model classes alone. But that approach skips most of the code common to iOS apps, especially with UIViewControllers. Learn how to unit test these view controllers to expand your unit testing possibilities. Since good unit tests form the bedrock for safe refactoring, you’re empowered to make bold changes. Learn how to avoid the most common mistakes Swift programmers make with the XCTest framework. Use code coverage to find holes in your test suites. Learn how to identify hard dependencies.

Reshape the design of your code quickly, with less risk and less fear.

What You Need

Xcode 10

Contents & Extracts

This book is currently in beta, so the contents and extracts will change as the book is developed.

  • Foundations
    • Assert Yourself excerpt
      • What Are Unit Tests, Anyway?
      • Creating a Place to Play with Tests
      • Writing Your First Assertion
      • Adding a Descriptive Message
      • Avoiding Conditionals in Tests
      • Describing Objects Upon Failure
      • Testing for Equality
      • Testing Equality with Optionals
      • Fudging Equality with Doubles and Floats
      • Avoiding Redundant Messages
      • Choosing the Right Assertion
      • Exercises
      • What’s Next?
    • Manage Your Test Life Cycles
      • Making a New Place to Play
      • Starting From Test Zero
      • Hooking Up Tests to Production Code
      • Examining Console Output
      • Observing Object Life Cycles to Learn the Phases of a Test
      • It’s a Trap: Reducing Duplicate Test Code the Wrong Way
      • Learning How XCTest Manages Test Cases
      • Using setUp() and tearDown()
      • Exercises
      • What’s Next?
    • Measure Code Coverage and Add Tests
      • Making a New Place to Play
      • Enabling Code Coverage
      • Examining Code Coverage Results
      • Drilling Into a Partially Covered Line
      • Adding Tests for Existing Code
      • Covering a Conditional
      • Covering a Loop
      • Covering Statements in a Sequence
      • Avoiding Targets, Embracing Forward Movement
      • Exercises
      • What’s Next?
    • Take Control of Application Launch
      • Making a New Place to Play
      • Observing the Default Behavior
      • Learning About the Test Launch Sequence
      • Bypassing the Normal App Delegate
      • Loading the Root View Controller Explicitly
      • Tweaking Your Testing App Delegate
      • Exercises
      • What’s Next?
    • Load View Controllers
      • Making a New Place to Play
      • Setting Up a Storyboard-Based View Controller for Experiments
      • Loading a Storyboard-Based View Controller
      • Setting Up a XIB-Based View Controller for Experiments
      • Loading a XIB-Based View Controller
      • Setting Up a Code-Based View Controller for Experiments
      • Loading a Code-Based View Controller
      • Exercises
      • What’s Next?
    • Manage Difficult Dependencies excerpt
      • Being Okay with Problem-Free Dependencies
      • Identifying Difficult Dependencies
      • Creating Boundaries to Isolate Dependencies
      • Making a New Place to Play
      • Adding Back Doors to Singletons You Own
      • Subclassing and Overriding: A Legacy Code Technique
      • Injecting Instances through Initializers or Properties
      • Injecting Closures to Make New Instances
      • Exercises
      • What’s Next?
  • iOS Testing Tips & Techniques
    • How Can I Ensure My Outlets Remain Connected?
      • Making a New Place to Play
      • Testing Outlet Connections
      • Checking the Effectiveness of Failure Messages
      • Exercises
      • What’s Next?
    • How Do I Tap a Button? (Intro to Actions) excerpt
      • Making a Place to Play with a Button
      • Testing Button Taps
      • Exercises
      • What’s Next?
    • What Can I Do with Alerts?
      • Making a New Place to Play
      • Testing Alerts
      • Exercises
      • What’s Next?
    • How Do I Test Navigation Between Views?
      • Making a New Place to Play
      • Testing Navigation
      • Exercises
      • What’s Next?
    • How Do I Test UserDefaults? (Intro to Fake Objects)
      • Making a New Place to Play
      • Isolating UserDefaults with Dependency Injection
      • Extracting a Protocol to Support Test Doubles
      • Making a Fake Object
      • Testing UserDefaults
      • Exercises
      • What’s Next?
    • How Do I Test Network Requests? (Intro to Mock Objects)
      • Making a New Place to Play
      • Isolating URLSession with Dependency Injection
      • Extracting a URLSession Protocol for Test Doubles
      • Making a Test Spy
      • Designing the Test Case
      • Promoting the Test Spy into a Mock Object
      • Improving Mock Object Reporting
      • Exercises
      • What’s Next?
    • How Do I Test Network Responses? (Intro to Closures)
      • Making a New Place to Play
      • Let’s Parse the Response
      • Starting with a Fresh Test Spy
      • Designing the Test Case
      • How Do I Test Asynchronous Code?
      • Exercises
      • What’s Next?
    • How Do I Test Text Fields? (Intro to Delegate Methods)
      • Making a Place to Play
      • Testing the Outlets
      • Testing Attributes and Wrangling UIKit Descriptions
      • Testing Delegate Methods
      • Testing Input Focus
      • Exercises
      • What’s Next?
    • How Do I Test Table Views?
      • Making a Place to Play
      • Testing Table Views
      • Exercises
      • What’s Next?
    • How Do I Test How My Views Look? (Intro to Snapshots)
      • Making a Place to Play
      • Adding FBSnapshotTestCase to a Test Target
      • Setting the Location for Reference Images
      • Writing a Snapshot Test
      • Seeing the Difference in a Snapshot Failure
      • Managing Your Snapshot Tests
      • Exercises
      • What’s Next?
  • Using Your New Power
    • Unleash the Subtle Power of Refactoring
      • Laying Out the Views for Our Practice App
      • Adding the Code to Our Practice App
    • Refactoring: Moving from MVC to Model View View-Model (MVVM)
    • Refactoring: Moving from MVC to Model View Presenter (MVP)
    • Refactoring: Extracting a State Machine
    • Test-Driven Development Beckons to You

Author

Jon Reid is a leading expert in unit testing and Test-Driven Development for iOS development. He has been practicing TDD on Apple platforms since 2001. His blog Quality Coding focuses on iOS unit testing practices.