At this point I’m going to presume that you have some form of iOS project setup, at this point it doesn’t really matter how simple or complex it is — just as long as there are some components that we can validate on the screen. Before we get started though, we need to setup our first test class. When you first created your project, Xcode should have generated a test class for your UI tests by default (called YourAppName_iOSUITests) — if it isn’t there for some reason, you’ll need to create one. It will need to look something like this:

import XCTest class SampleApp_iOSUITests: XCTestCase { }

You’ll notice here that our class extends the XCTestCase class — this is the foundation for defining a class that defines a collection of tests. It’s important to note that the test classes you create should be focused — if we aim to group similar responsibilities per test class then we can ensure that these classes will remain fine-grained and easy to maintain. With this in mind, naming your test classes per-responsibility will not only help to enforce this, but also help when navigating your project.

Next we need to define a global reference to our Application instance that will be used during testing — here we define this field as an XCUIApplication instance. This global instance allows us to perform interactions with our test application instance from each test — this allows us to launch / terminate / set state as we require.

var app: XCUIApplication!

Now that we’ve defined this, we’re going to override the XCTestCase setUp function so that we can initialise this instance and configure any other parts that are required for our tests.

override func setUp() {

super.setUp()

continueAfterFailure = false

app = XCUIApplication()

app.launchArguments.append("--uitesting”)

}

Let’s take a look at what each part here is doing: