Testing iOS Applications

In this series we'll learn how to test iOS applications with XCTest. We will see the benefit of real-time feedback from the code that we are writing, ensuring that the pieces work before integrating them into the real application. Testing can also give us the benefit of making sure that our code still works as we add new features, as new iOS versions and devices are released. Having a solid test suite can enable refactoring with confidence, as we can know quickly if something is not working as intended. You will learn how to take advantage of mock objects, how to test networking code, asynchronous code, and more.

Length: about 3 hours.

Start Series


1. Intro

Subscribers only

Introduction to the Testing iOS Applications series

2. Introduction to XCTest

Subscribers only

XCTest is a test framework built into Xcode. In this episode we'll see the mechanics of how this works, how your test bundle sits alongside your app in Xcode. We'll look at the structure of test cases, and the assertions we will use to verify behavior of our code.

3. Adding XCTest to Existing Projects

Subscribers only

If you did not check the box "Include Unit Tests" when first creating your project (or perhaps you want to add a separate testing target), this screencast will show the steps of how to get it set up from scratch. It also covers how to set up a scheme dedicated to building your tests, and choosing which tests to run automatically.

4. Using XCTest in Playgrounds

Subscribers only

Running your tests in Xcode Playgrounds can be a great way to get rapid feedback on your changes. It is a useful way of practicing testing and experimenting with APIs that you are not familiar with. In this episode we will see how to run the test suite and how to enhance the feedback we get by observing the test results as they are run.

5. Value Based Testing

Subscribers only

In this episode we go over examples of value based testing used for easily testable systems.

6. Mocks and Stubs

Subscribers only

In this episode we use mocks and stubs to setup collaborating objects to inspect the behaviors of tested objects.

7. Testing Asynchronous Code

Subscribers only

In this episode we cover the concept of expectations, which enables us to test asynchronous code, properly timing out and failing a test if the expectation is never fulfilled.

8. CoinList: Testing a real API

Subscribers only

In this episode we talk about testing requests against a real API. For this we will build an app called CoinList that leverages the Crypto Compare API to fetch stats about crypto currencies.

9. CoinList: Stubbing Network Requests

Subscribers only

In this episode we implement OHHTTPStubs, a library that can be used to intercept and stub out network calls made with URLSession. Using this technique we can avoid hitting the network for our tests. We can also simulate different responses that are difficult or impractical to simulate in a real request.

10. How to Measure Test Coverage with XCTest

Subscribers only

In this episode we look at Xcode's support for measuring Code Coverage, a term that describes how much of your code is exercised during a test run. Using this tool you can see where your deficiencies are and add tests as needed to cover more of your production code with tests.

11. Refactoring Tests - Using #file and #line to indicate failure location

Subscribers only

When refactoring tests, you end up moving critical assertion logic outside of the test method. This can cause our tests to fail in the wrong spot. This becomes worse if multiple methods share test logic. When a test fails you want to know exactly where the failure occurred. By leveraging #file and #line expression literals we can move the failure back to where it should be, within the test method. We will also see how we can continue to use expectations outside of a test instance.

12. Creating Higher Level Assertions

Subscribers only

XCTAssertion functions are a foundational part of your test suite but sometimes can be too low-level. You want to know exactly why a test failed, not that "2 was not less than 1." By extracting helper methods to do assertions for you, you can give groups of assertions meaning, and make it easier to reason about your tests and why they might be failing.

13. Testing View Controllers - Loading Data

Subscribers only

Testing view controllers can sometimes be challenging. In this episode we will write some tests that verify a view controller loads its data properly from the API client. We will add additional tests to verify that a loading indicator is shown.

14. Testing our Table View Datasource Implementation

Subscribers only

We continue testing our view controller, this time focusing on the UITableViewDataSource implementation. Testing the number of rows returned is easy, but we can also test that the controller returns the appropriate cell and that its outlets are configured properly.

15. Detect When Running Tests in Your AppDelegate

Subscribers only

When running your unit tests, you may have noticed that your application continues to launch in the simulator. In this episode we'll talk about why that can be problematic and what you can do to mitigate this issue from interfering with your tests.

16. Stubbing Image Requests

Subscribers only

In this episode we will intercept and stub image requests to prevent our tests from making network calls when interacting with our view controller.