UI Tests with XCTest in SwiftTue Jul 20 2021


Writing UI tests in Swift has never been easier. Tests use the XCTest library. Currently they are run locally with soon with XCode Cloud Apple will host and run them on our behalf.


There are not many resources online to get started. Two posts that helped were Xcode UI Testing Cheat Sheet by Paul Hudson and Getting started with Xcode UI testing in Swift by John Sundell.


While writing tests, breakpoints are your best friend. Once the code is halted. $ po app will print the view hierarchy. To simulate an action $ e app.….tap() executes a command. This will effect the simulator.

⌘ + 9 in XCode goes to tests that have run. From there you can see the lines that have executed and screenshots of the app at the failure.

Network Requests

Requests are slow and the simulator needs to wait for them. To solve this create a loop of sleeps until the task is complete.

class UITests: XCTestCase {
    var app: XCUIApplication!
    var sleepCounter = 0

    override func setUp() {
        continueAfterFailure = false
        app = XCUIApplication()
        sleepCounter = 0

    func myTest() {

        sleepCounter = 0
        // myChecker - any XCTAssert
        while ![myChecker] && sleepCounter < 40 {
            sleepCounter += 1

It is important to call the assertion after the loop so if the case hasn't hit yet it will show a failure. Additionally enter the max amount of time to wait before a response. In our test case it is 40.