© Avi Tsadok 2020
A. TsadokPro iOS Testinghttps://doi.org/10.1007/978-1-4842-6382-2_11

11. Using Command-Line Tools

Avi Tsadok1 
(1)
Tel Mond, Israel
 

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.

In this chapter, you will learn
  1. 1.

    What CI/CD means and how tests fit it

     
  2. 2.

    What are the command-line tools provided by Xcode

     
  3. 3.

    Installing and setting up the xcodebuild application

     
  4. 4.

    How to run tests from the command line

     
  5. 5.

    How to run test plans from the command line

     
  6. 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.

But if you still want to download them, you have two options:
  • Download from the Developer website.

  • Install from the command line using xcode-select.

“xcode-select” is a command line that comes bundled with macOS. If you want to use it to install Command-Line Tools, open the Terminal and type
$ xcode-select –install

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.

To find out what is the “active” Xcode version, we can use xcode-select again for that:
$ xcode-select -p
/Applications/Xcode.app/Contents/Developer
To change the active Xcode version, locate the developer path and use the -switch command:
$ xcode-select -switch /Applications/Xcode12.app/Contents/Developer
To uninstall Command-Line Tools, just delete /Library/Developer/CommandLineTools with this command:
$ sudo rm -r /Library/Developer/CommandLineTools

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.

A basic xcodebuild command looks something like this:
$ xcodebuild
 -workspace MyWeatherApp.xcworkspace
 -scheme MyWeatherApp
 -destination 'platform=iOS Simulator,name=iPhone 11'
 test
Let’s go over the command parameters:
  • 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.

This is the list of platforms you can use:
  • 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:

To run your test on iPhone 11 Simulator, running iOS 12.0:
-destination "platform=iOS Simulator, name=iPhone 11, OS=12.0"
To run your test on a physical device:
-destination "platform=iOS, name=Avi's iPhone"
To list all of your available destinations, type in your terminal:
$ instruments -s devices

Run Test Plans from Command Line

You can use xcodebuild to run test plans right from the command line.

To see the list of available test plans for a scheme, use showTestPlans argument:
$ xcodebuild -scheme 'My Weather App' -showTestPlans
Test plans associated with the scheme "My Weather App":
        Localization Test Plan
        Memory
Running tests with a specific scheme will run the default test plan. To run a particular test plan, use testPlan argument :
$ xcodebuild
 -workspace MyWeatherApp.xcworkspace
 -scheme MyWeatherApp
 -destination 'platform=iOS Simulator,name=iPhone 11'
 -testPlan 'Memory' test

More xcodebuild Important Arguments

“xcodebuild” has more tricks up in its sleeves.

To list all the schemes, build configurations, and targets, use list argument :
$ xcodebuild -list
Information about project "My Weather App":
    Targets:
        My Weather App
        My Weather AppTests
        My Weather AppUITests
    Build Configurations:
        Debug
        Release
    If no build configuration is specified and -scheme is not passed then "Release" is used.
    Schemes:
        My Weather App
If you already build your app for testing and want to rerun tests without building it again, you can use run-without-building to save time:
$ xcodebuild
 -workspace MyWeatherApp.xcworkspace
 -scheme MyWeatherApp
 -destination 'platform=iOS Simulator,name=iPhone 11'
 run-without-building Test
On the other hand, if all you want is to build but not test, you can use build-for-testing argument :
$ xcodebuild
 -workspace MyWeatherApp.xcworkspace
 -scheme MyWeatherApp
 -destination 'platform=iOS Simulator,name=iPhone 11'
 build-for-testing Test
If you want to make sure Xcode cleans the project before running your tests, you can add clean to the command:
$ xcodebuild
 clean
 -workspace MyWeatherApp.xcworkspace
 -scheme MyWeatherApp
 -destination 'platform=iOS Simulator,name=iPhone 11'
 test
To see all the available SDKs you can use, try the showsdks argument :
$ xcodebuild -showsdks
iOS SDKs:
        iOS 13.2                      -sdk iphoneos13.2
iOS Simulator SDKs:
        Simulator - iOS 13.2          -sdk iphonesimulator13.2
macOS SDKs:
        DriverKit 19.0                -sdk driverkit.macosx19.0
        macOS 10.15                   -sdk macosx10.15
tvOS SDKs:
        tvOS 13.2                     -sdk appletvos13.2
tvOS Simulator SDKs:
        Simulator - tvOS 13.2         -sdk appletvsimulator13.2
watchOS SDKs:
        watchOS 6.1                   -sdk watchos6.1
watchOS Simulator SDKs:
        Simulator - watchOS 6.1        -sdk watchsimulator6.1

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.33.207