Running Tests on the Command Line

One thing that may have occurred to you is that all the tests run up to this point have been performed in the Xcode GUI, either by clicking gutter buttons in the editor view, run buttons in the Test navigator, or with the menu item Product > Test (U). That’s all well and good, but it’s obviously inadequate if you want your tests to be part of a continuous integration (CI) process, one which automatically runs tests for you, either periodically (like every night), or maybe every time new code is checked in.

Fortunately, it’s simple to have Xcode run tests from the command line or in a shell script. It’s actually a lot like automated building of your project, which you saw way back in Building on the Command Line. In fact, it uses the same command, xcodebuild. The difference is that xcodebuild implicitly takes an action, which defaults to build. To run tests, you just need to use xcodebuild test.

Except… it’s not actually that simple. The test action doesn’t settle into sensible defaults, so just using xcodebuild test doesn’t work:

=> xcodebuild test
<= xcodebuild: error: The test action requires that the name of a scheme in the
 project is provided using the "-scheme" option. The "-list" option can be used
 to find the names of the schemes in the project.

The hint is nice, as it tells you that xcodebuild -list will give you the name of all the schemes, one of which you’ll include with the command next time. As it turns out, that’s only half the story…

=> xcodebuild -scheme AddTestsLaterDemo test
<= xcodebuild: error: Failed to build project AddTestsLaterDemo with
 scheme AddTestsLaterDemo.
  Reason: A build only device cannot be used to run this target.

Right. So you need to not only provide a scheme, but also specify either an actual iOS device by name, or the identifier for one of the installed simulators. You can use the action showdestinations to show strings for all the available simulators, and then pick the one to run on:

=> xcodebuild -scheme AddTestsLaterDemo -showdestinations

Notice that you have to include which scheme you’re interested in, since you may have some simulators that are incapable of running a given scheme (maybe your app is iPhone- or iPad-only, or you have simulators older than the app’s minimum supported iOS version). The output of this command is a list of simulators, which are identified by id, OS version, and name. It’s a long list, so here’s just a clip:

<= Available destinations for the "AddTestsLaterDemo" scheme:
  { platform:iOS Simulator, id:A1EF4A02-5A4E-4B88-8D11-E931C8FD7809,
  OS:11.4, name:iPad (5th generation) }
  { platform:iOS Simulator, id:68D194AF-D16A-4A5E-95A2-70748375DA07,
  OS:11.4, name:iPad Air }
  { platform:iOS Simulator, id:9BF0DC53-036E-4A40-A19C-E2A62A3C044D,
  OS:11.4, name:iPad Air 2 }
  { platform:iOS Simulator, id:DB0B5527-0155-4C80-89B1-1FDE004A11E6,
  OS:11.4, name:iPad Pro (9.7-inch) }

The way to compose the -destination option is to provide a set of criteria that xcodebuild can match. You could use the id string, but it differs from computer to computer, so that approach won’t work for teams. Instead, you should compose your destination as a combination of platform, OS, and device name, like the following command (which has added a backslash line-continuation character to fit the book’s limited width):

=> xcodebuild -scheme AddTestsLaterDemo -destination
=>  platform='iOS Simulator',OS=11.4,name='iPhone 8' test

This will run the tests on the specified simulator. If all of the tests pass, xcodebuild returns 0, like any other command-line process does when it ends normally. On the other hand, if any tests fail, the return value will be non-zero (usually 65), so you can watch for that in build scripts and CI systems.

What if you want to run your tests on a device? That’s when you use the id, since now you’re talking about one specific iOS device, as opposed to one instance of a simulator. You can get the device identifier by bringing up the Devices and Simulators window (either from the Window menu or with 2), as seen in the figure. Plug in your device via USB, select it from the Devices tab, and copy-and-paste the “Identifier” string:

images/testing/xcode-devices-and-simulators-device-id.png

With the device identifier in the clipboard, you can paste it into the Terminal as you enter the xcodebuild command, which will look like this:

=> xcodebuild -scheme AddTestsLaterDemo -destination id="YOUR_DEVICE_ID" test

Where We’re Going, We Don’t Need… Cords

images/aside-icons/tip.png

One really cool feature is hiding here in the Devices and Simulators window: the check box marked “Connect via network”. Selecting it allows Xcode to run, test, and debug the app over Wi-Fi, without being connected via a USB cable.

Once you enable Connect via network, the device can be unplugged and will still appear in the Devices and Simulators window and the scheme selector—as long as it’s within Wi-Fi range—and will have a little globe icon to indicate it is connected wirelessly.

images/testing/xcode-devices-and-simulators-wifi-device.png
..................Content has been hidden....................

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