The most important practice for continuous integration to work properly is frequent check-ins to trunk or mainline. You should be checking in your code at least a couple of times a day.
—David Farley
Introduction
While it’s very convenient to use the Xcode UI interface to configure and run tests, it’s impossible to do that when you want to run them on a remote server.
Fortunately, every new Xcode installed comes with a tool named “Xcode Command-Line Tools” to help you run your tests without any UI interface.
This chapter concludes the final piece in the puzzle by giving you the tools to integrate your great test suites into a continuous integration environment.
- 1.
What CI/CD means and how tests fit it
- 2.
What are the command-line tools provided by Xcode
- 3.
Installing and setting up the xcodebuild application
- 4.
How to run tests from the command line
- 5.
How to run test plans from the command line
- 6.
Exciting and useful xcodebuild features
What Is CI/CD Anyway?
“CI” and “CD” often come together – “CI/CD.” However, these are two different processes:
CI (Continuous Integration) – In a continuous integration process, we take the code from all the developers/third party/server changes and merge it together to a new working build. The goal of this process is to make sure this build is stable and nothing broke along the way.
CD (Continuous Deployment) – In “Continuous Deployment,” we upload the build to an online service and make sure we have an available app to download. In fact, CD is an extension of the CI process and cannot stand by itself.
How Tests Fit In?
Based on the preceding data, you can understand that tests are a central part of this process. In fact, a continuous integration without any tests involved is like dressing up, taking a babysitter, and going out to a restaurant for a glass of water – it just doesn’t worth it.
There are plenty of tools that can help you integrate your tests automatically in a CI environment, but they all rely on Xcode command-line tools to run them.
Command-Line Tools
Command-Line tools are a great way to script your testing and incorporate it with CI/CD environments. They let you build, archive, and test your projects from the terminal command line and customize your run with different parameters and arguments.
Command-Line Tools are not just for CI/CD – they can also help you automate your tasks during development. For example, instead of repeating the same actions of testing different test plans, committing, and then archiving, you can implement all of that in one script file.
Meet xcodebuild
“xcodebuild” is the primary tool of the command-line tool package, and it’s used to build, archive, and analyze test and any action you can do with the scheme.
The real power of xcodebuild is the flexibility to run various actions and configurations and, as a result of that, it is the primary tool for testing.
Install and Set Up xcodebuild
Although you can download Command-Line Tools separately, they come with every new Xcode.
Download from the Developer website.
Install from the command line using xcode-select.
And press Enter.
Many developers have multiple versions of Xcode installed on their machine, and one of the first steps using xcodebuild is to make sure it works with the correct Xcode version.
Run Tests with xcodebuild
Running tests with xcodebuild is quite simple and requires very few parameters for the basic run. First, you need to make sure your current directory in the Terminal is the project directory.
workspace – If you are using workspaces instead of projects (CocoaPods is a good example), pass your workspace name here.
scheme – Your selected scheme name.
test – Run the “test” action of the scheme.
destination – Specify the platform that is used for the test. A destination can be either a simulator or a physical device. Let’s go deeper into this.
The Destination Argument
The syntax of the destination argument is based on key–value pairs. The first key is the platform, which describes whether it’s a device or a simulator and what platform it is.
OS X, your Mac
iOS, a connected iOS device
iOS Simulator
watchOS
watchOS Simulator
tvOS
tvOS Simulator
The second key–value pair is related to the type of the device.
If it’s a physical device, you can use either “name” to target the actual device name or “id” to target the device UUID.
If it’s a simulator, the “name” key describes the name of the simulator (“iPhone 11”), and another key–value pair is “os” to specify the OS version (“11.0”).
Let’s see some examples:
Run Test Plans from Command Line
You can use xcodebuild to run test plans right from the command line.
More xcodebuild Important Arguments
“xcodebuild” has more tricks up in its sleeves.
Summary
We’ve learned that it’s not enough to write great tests; it’s also essential to make sure to run them continuously. As an iOS developer, we need to focus on writing great software and solve complex problems. Let the automation server take care of running the tests for us.