# Run Xcode tests in a macOS machine

Tests run in a throwaway clone of a macOS template that has your Mac's Xcode and the iOS Simulator.

## Make the Xcode template

Requires [OSMates Pro](https://osmates.com/docs/editions#what-pro-adds).

Before you start, install Xcode in `/Applications` and leave about 90 GB free. Creation downloads macOS and about 8 GB for the Simulator, so it needs a network connection.

To make a template named `xcode`, run this, then type the account's password and press Return:

```sh
osmates vm create xcode --image macos-xcode --password-stdin --agree-to-xcode-license
```

`--agree-to-xcode-license` says you agree to the license Xcode shows on your Mac. That license covers Macs you own or control only, so don't share the template or a disk exported from it. To copy another Xcode, add `--xcode /Applications/Xcode-beta.app`.

Creation installs macOS, copies Xcode in, downloads the Simulator, and makes `xcode` a template. Its account signs in by itself, so tests run with nobody at the machine. Nothing signs in to an Apple Account. New Machine doesn't offer this template, so make it in Terminal.

## Run tests in a throwaway clone

Each run clones the template, copies your project in, runs the tests on the account's desktop, and deletes the clone. The account has your Mac's user name, so `$USER` names its home folder.

To list the simulators, then test `MyApp` on one of them:

```sh
osmates run xcode --desktop -- xcrun simctl list devices available
osmates run xcode --desktop --copy-in ./MyApp:/Users/$USER/MyApp --copy-out /Users/$USER/MyApp.xcresult:./MyApp.xcresult -- xcodebuild test -project /Users/$USER/MyApp/MyApp.xcodeproj -scheme MyApp -destination 'platform=iOS Simulator,name=<simulator>' -resultBundlePath /Users/$USER/MyApp.xcresult
```

Replace `<simulator>` with a name from the first command's list. `osmates run` exits with the status of `xcodebuild`. The result bundle comes back to `./MyApp.xcresult`, also when a test fails.

## Keep the machine identifier

Xcode knows a Mac by its provisioning UDID. A clone gets a new one, so an app signed with a provisioning profile for the template doesn't run in it. To keep it, keep the machine identifier:

```sh
osmates vm clone xcode xcode-2 --keep-identifier
```

In New Machine, select the template, then choose **Keep Its Identifier** under **Identity** before you click **Create**.

![New Machine from the template xcode, with Keep Its Identifier selected under Identity](https://osmates.com/docs/images/macos-xcode-keep-identifier.png)

Only one machine with an identifier runs at a time. Starting a second one is refused with the name of the one that runs. The source needs to have started once, which makes its identifier.

## Use a newer Xcode

A template keeps the Xcode it copied. To use a newer Xcode, delete the template and make it again.
