By Blake Lucchesi on May 11, 2014

Using xcodebuild to Automatically Run Tests on a Connected Device

I recently spent some time putting together a continuous integration setup using a Mac Mini, an iPhone 4s and GitlabCI. In order to have our tests run via the command line I used the very powerful xcodebuild terminal command. Something that I wanted to do as part of this was build a wrapper script so that the test suite could be run not only on our CI server, but on our laptops as we develop. In order to tell xcodebuild that you want it to run on a device instead of the simulator, you need to provide the -destination flag like so:

$ xcodebuild test -destination 'id=[DEVICE ID]' ...

So this left me wondering, how can I dynamically get a UDID from whatever device we may have connected to our laptop or build server. The solution I came up with was to use the system_profiler command and limit the results by USB connected devices.

$ system_profiler SPUSBDataType

If you run this command you’ll quickly realize that we get a lot of output. Luckily there is a pretty simple regex that we can run to get just the UDID from the connected iDevice that we care about. An example of this is shown below, implemented in Ruby.1

  1. I use a Ruby tool called Rake to wrap our build/test commands, more on that in a future post.

comments powered by Disqus