Make the right assertions in iOS unit tests for proper dev

Unit tests play an integral role in mobile app development. Here's an excerpt from 'iOS Unit Testing by Example' about why fast feedback and control can help testers.

Effective iOS unit tests can represent a means to refactor code and practice test-driven development for the mobile OS. In fact, understanding iOS specifics is crucial to a successful implementation of test-driven development; having just a grasp of the conceptual details won't work.

That's the case Jon Reid -- a technical Agile Coach -- makes in his book iOS Unit Testing by Example. For instance, Reid argues that testers must understand how to navigate the nuisances of the Swift XCTest framework and know about common dependencies in iOS code.

Reid believes that iOS unit tests are valuable because of the speedy feedback and high measure of control they provide. It's that fast feedback that enables effective refactoring and, accordingly, allows developers to put test-driven development into practice.

Here's a taste of iOS Unit Testing by Example, the title's coding exercises and step-by-step instructions.

Create a place to play with tests

In this section of Chapter 1, Reid walks through how to start a new project in the Xcode IDE, where unit tests in the XCTest framework happen. He stresses the importance of test assertions:

'iOS Unit Testing by Example' book coverClick to learn more about the title

"Assertions give unit tests a way to state their expectations. The tests fail if these expectations aren't met. Let's make a place outside of your actual projects where we can experiment with how they work."

In an Xcode project, testers must use the Swift programming language, choose Storyboard -- as opposed to SwiftUI -- as the UI and check off the box Include Unit Tests.

This new project should create an initial test file, and its file name will be the title of the project with "Tests" at the end. Testers should then create an empty shell by deleting every method in the file's class and set the destination to be an iOS simulator.

At this point, testers can technically tell Xcode to perform the unit tests. But the IDE won't indicate any test failures since no tests exist yet. In subsequent parts of the chapter, Reid goes on to explain how to actually create iOS unit tests and how the right assertion can play a major role in quality testing.

Click here to read Chapter 1 of iOS Unit Testing by Example.

Next Steps

3 common challenges of mobile application testing

Why is unit testing important for developers?

Dig Deeper on Software testing tools and techniques