Chapter 18. UI Tests

We spoke about unit tests back in “Testing the SelfieStore”, but there is another type of testing Xcode can perform. UI tests are a way to run tests on the UI itself. Up to this point, any time we’ve wanted to test something in our UI, such as toggling the states of the settings switches or testing the UI flow for our custom capture view controller, we have had to do so manually. The problem with this is that it obviously doesn’t scale very effectively and can get very boring very quickly for the developer. UI tests are an answer to this: essentially, they give you access to a virtual hand you can script, giving it orders and expectations for it to fulfill. It’s like having your own army of testers you can make do all the tapping and swiping about for you.

Warning

Throughout this chapter we will be making some assumptions about the permissions and setup of Selfiegram. Normally you would not do this and instead would provide setup code to ensure everything is ready for the tests, but as we are interested in showing off how to write UI tests and not general testing methodology we are skipping some of this. In your own apps, make sure you do perform your test setup correctly!

UI Testing Classes

There are three main types we interact with when doing UI testing:

XCUIApplication

A proxy object for the app being tested (in our case, Selfiegram).

XCUIElement

A single UI element we can interact with and query. This can be almost any type, including buttons.

Get Learning Swift, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.