iOS Unit Testing by Example

XCTest Tips and Techniques Using Swift

“Finished reading Jon Reid’s iOS Unit Testing by Example, a wonderfully clear and no-frills demonstration of testing practices. This book should be considered table stakes for iOS developers. ⭐️⭐️⭐️⭐️⭐️”

Paul Goracke

Senior iOS Engineer

iOS Unit Testing by Example

By Jon Reid

Topics Covered by iOS Unit Testing by Example

Foundations:

What are the basic tools and skills you need to write unit tests for iOS code?

There are subtleties around how unit tests work in XCTest. They’re not difficult but are often overlooked. Let’s get this right to avoid problems down the road.

As we get into iOS specifics, it’s helpful to know how your normal application launch affects testing. And you’ll need to know how to load your view controllers so you can test them.

But typical iOS code contains dependencies that interfere with testing. Let’s learn how to identify which dependencies are problematic and how to box them off. Previously untestable code will become testable.

iOS Testing Tips and Techniques:

With foundations in place, how do we test specific behaviors of iOS apps?

Some of this requires tricks. You can test a button tap or navigation from one view controller to the next with a handful of tricks.

Some of it requires learning how to use test doubles to replace an actual component with a stand-in. This includes fakes, spies, and mocks.

And besides testing behavior, it’s good to know how to test appearance.

This section offers a grab bag of tips and techniques that you can look up as needed.”

Refactoring:

Why is all this unit testing useful? The speed of unit test feedback empowers powerful styles of development.

Fast feedback is the engine that powers disciplined refactoring: working in small, verified steps. Refactoring gives us a way to change the design of code without changing its behavior. We’ll see an example of refactoring a view controller’s design from MVC to MVVM, then on to MVP.

Intro to Test-Driven Development:

If we flip conventional coding-then-testing on its head, we get test-driven development. We will see how TDD combines a test-first approach with refactoring. The result is a powerful way to develop code.

click to play

Not Covered in This Book

Disclaimer: No SwiftUI Support

SwiftUI was still very new when I finished writing this book, so it’s not included. But I am starting a new series I’m calling “TDD in a SwiftUI World”. So be sure to subscribe to get new articles as I write them!

Get Your Copy of iOS Unit Testing by Example…

Reader's reviews

My Big 5 takeaways from iOS Unit Testing by Example

by Dan Smith

My entire plan for today is to read Jon Reid’s new book on unit testing cover to cover. For years Jon has provided thoughtful, high-quality, and entertaining articles about testing on his site, and I have no doubt this book will rock – always keep learning, folks!

Paul Hudson, Hacking with Swift

I desperately needed this book 3-4 years ago, but I’m glad to have discovered it now. Thank you @qcoding

kern

A must-read for anyone looking to start unit testing

John Yorke

Must read for any iOS dev

Whether you’re starting out in testing or already have a little bit of experience in the matter under your belt, this is a must read book.

One of the nicest thing it has is that it allows scanning it through until you find that little nugget of information you we’re looking for

Mauricio Chirino

Amazing testing resource for iOS - start here

There are a lot of hands-on and practical tips in this book as you write unit tests.

Need to disable logic in your tests from your actual app? You’ll learn about that here.

Want to make tests more reliable? Get this book.

I highly recommend reading it and leveraging the key insights that Jon shares. He also does occasional videos and shared tips through his newsletter.

Paul Solt

Fantastic Resource for iOS Unit Testing

I've been looking for detailed Unit Testing examples for iOS online for a while before I finally decided to break down and get a book on the subject. The main reason was that I needed to find a subject matter expert that I trusted to give me the insight that I needed to set up my own tests. Jon's website and blog articles on unit testing proved that he has the knowledge, and that he's a gifted teacher.

I highly recommend this book for anyone looking to learn how the iOS unit testing framework can be best put to use in your own applications. Jon was able to explain how to set up simple unit tests to test complex objects and user interfaces, all while explaining the basics of unit testing and providing a valuable toolkit for tackling the reader's specific challenges.

Cheesmo

Finally a book that covers our day to day testing situations.

Amazon Customer

This is the best book about testing iOS Apps I have read so far

This book is a complete guide about iOS Testing, every Software Engineer working with Apple platforms would greatly benefit from it. You'll learn about how to write Unit, Integration and Snapshot tests for native iOS components, VCs and many more.

The book is well organized, from the more basics parts of iOS all the way to TDD and Refactoring techniques. The example code runs and works, no surprises there.

I learned so much and highly recommend it.

Noel C Perez

Every iOS developer should read this book!

Ben Chatelain

Finished reading @qcoding’s iOS Unit Testing by Example, a wonderfully clear and no-frills demonstration of testing practices. This book should be considered table stakes for iOS developers. ⭐️⭐️⭐️⭐️⭐️

Paul Goracke

If you are an iOS developer, you should get this book. Jon is THE expert on unit tests for iOS. I learned so much from him.

Dominik Hauser

Your book is amazing as I had absolutely no idea how to unit test or refactor stage by stage as you’ve shown it through your book. Worth every penny! 👍

Mo

“Code coverage can be misleading, but lack of coverage is always true”. Such a great assertion @qcoding!! Enjoying iOS Unit Testing by Example a lot!!

Raúl Martínez

A must-have collection of practical tips and techniques from the G.O.A.T of Unit Testing @qcoding

Franklin Byaruhanga

For this #FollowFriday I can highly recommend @qcoding!

I really enjoyed reading his book about unit testing iOS applications and you should definitely give it a try! Testing your code will make you a more professional developer and let you sleep at night.

Manuel Schulze

I've been leaning more into unit tests this year.

Without out a doubt, "iOS Unit Testing by Example" has helped tremendously.

Thank you, @qcoding

Dylan S.

@qcoding knows what's up. His book and material are great for learning how to get started with iOS testing and test-driven development. 💫

chefski

iOS Unit Testing by Example is a fantastic resource if you’re both a beginner or you’re writing tests for a long time.

The writing of the book is super accessible and the format should be adapted by more authors.

Great work @qcoding!

Agis Tsaraboulidis

Jon has done a terrific job at sharing tips and techniques in bite sized practical chunks. There’s something for everyone, beginners to experts. A must have.

Gio

I am getting tons of benefit from this great book by @qcoding. Recommended for all iOS developers who are on the TDD fence, but still want to sharpen unit testing skills and techniques.

Amber Star

Let Me Tell You More About
iOS Unit Testing by Example

One of the principles of agile software development is to “respond to change.” Agility sounds appealing, but these days it is often approached as a project-management tool. Yet we are developers, working in code. The more the code itself resists change, the harder it will be to adopt agility in any meaningful way.

But we can change this—because we are developers, working in code. There are disciplines that fall under the term “technical agile practices.” Among these are unit testing, refactoring, and test-driven development. These are some of the tools we can use to make code pliable so we can safely bend that code into new shapes.

I’ve been learning to apply these disciplines in the workplace since 2001. Lately I’ve been teaching “Test-Driven Development for iOS” workshops. So it seemed a safe bet that if I ever wrote a book, it would be about TDD.

But I realized that folks can learn theoretical TDD, but still be stuck on iOS particulars. Because if you don’t know how to write unit tests for iOS code, how can you ever TDD it? So my book plans pivoted, and here we are. My goal is to give you solid handholds so you can unit test your iOS code.

iOS Unit Testing by Example

By Jon Reid

You'll learn how to:

  1. 1
    Assert Yourself
  2. 2
    Manage Your Test Life Cycles
  3. 3
    Measure Code Coverage and Add Tests
  4. 4
    Take Control of Application Launch
  5. 5
    Load View Controllers
  6. 6
    Manage Difficult Dependencies
  7. 7
    Test Outlet Connections
  8. 8
    Test Button Taps (Using Actions)
  9. 9
    Test Alerts
  10. 10
    Test Navigation Between Screens
  1. 11
    Test UserDefaults (with Fakes)
  2. 12
    Test Network Requests (with Mocks)
  3. 13
    Test Network Responses (and Closures)
  4. 14
    Test Text Fields (and Delegate Methods)
  5. 15
    Test Table Views
  6. 16
    Test View Appearance (with Snapshots)
  7. 17
    Unleash the Power of Refactoring
  8. 18
    Refactoring: Moving to MVVM
  9. 19
    Refactoring: Moving to MVP
  10. 20
    Test-Driven Development Beckons to You

Get Your Copy of iOS Unit Testing by Example…

About Jon Reid

Jon Reid is a technical agile coach at Industrial Logic, and a leading expert in unit testing and test-driven development (TDD) for iOS development. He has been practicing TDD on Apple platforms since 2001. His blog Quality Coding focuses on technical agile practices for iOS developers, including unit testing, TDD, and refactoring.

“If you know anything about me, you probably know that I’m a unit testing nut. What you may not know is why. I’m committed to two things: increasing your joy as a coder, and driving down the cost of change for your business. These two things go together.”

Signed, Jon
Jon Reid

iOS Unit Testing by Example

By Jon Reid

Get Your Copy of iOS Unit Testing by Example…

>