Xcode5環境で、xcodebuildを使ってユニットテスト実行する方法

[Xcode5からxcodebuildでtestアクションが対応された](https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_5_0.html)らしい。

>The xcodebuild command-line tool now supports the test action for both iOS and OS X tests, allowing a scheme’s test action to be performed from the command line or integrated into other scripts. Detailed information on using xcodebuild for running tests can be obtained using man from a Terminal window. Type: man xcodebuild

Xcode4までは、Jenkins等コマンドライン環境からテストを実行するためには、[ios-simと連携](http://kkinukawa.hatenablog.com/entry/2013/04/12/012256)するなりxctool使うなりしなければならなかったのですが、Xcode単体でできるならば非常に敷居が下がるので嬉しい。

#やってみた
man見ろと書いてあるので、man見てやってみた。

```bash
$ xcodebuild -workspace KiwiSample.xcworkspace -scheme KiwiSample -destination 'platform=iOS Simulator,name=iPhone Retina (4-inch)' test

=== BUILD TARGET KiwiSample OF PROJECT KiwiSample WITH CONFIGURATION Debug ===

Check dependencies

=== BUILD TARGET Pods-KiwiSampleTests-Kiwi OF PROJECT Pods WITH CONFIGURATION Debug ===
Check dependencies

=== BUILD TARGET Pods-KiwiSampleTests OF PROJECT Pods WITH CONFIGURATION Debug ===

Check dependencies

=== BUILD TARGET KiwiSampleTests OF PROJECT KiwiSample WITH CONFIGURATION Debug ===

Check dependencies
...

Test Suite 'All tests' started at 2014-04-02 06:33:20 +0000
Test Suite 'KiwiSampleTests.xctest' started at 2014-04-02 06:33:20 +0000
Test Suite 'AsyncTest' started at 2014-04-02 06:33:20 +0000
Test Case '-[AsyncTest FetchingServiceData_ShouldReceiveDataWithinOneSecond]' started.
2014-04-02 15:33:21.027 KiwiSample[91052:60b] + 'Fetching service data, should receive data within one second' [PASSED]
Test Case '-[AsyncTest FetchingServiceData_ShouldReceiveDataWithinOneSecond]' passed (0.345 seconds).
Test Suite 'AsyncTest' finished at 2014-04-02 06:33:21 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 0.345 (0.346) seconds
Test Suite 'HogeSpec' started at 2014-04-02 06:33:21 +0000
Test Case '-[HogeSpec Math_IsPrettyCool]' started.
2014-04-02 15:33:21.029 KiwiSample[91052:60b] + 'Math, is pretty cool' [PASSED]
Test Case '-[HogeSpec Math_IsPrettyCool]' passed (0.001 seconds).
Test Suite 'HogeSpec' finished at 2014-04-02 06:33:21 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 0.001 (0.001) seconds
Test Suite 'KWSpec' started at 2014-04-02 06:33:21 +0000
Test Suite 'KWSpec' finished at 2014-04-02 06:33:21 +0000.
Executed 0 tests, with 0 failures (0 unexpected) in 0.000 (0.000) seconds
Test Suite 'MathSpec' started at 2014-04-02 06:33:21 +0000
Test Case '-[MathSpec Math_IsPrettyCool]' started.
2014-04-02 15:33:21.029 KiwiSample[91052:60b] + 'Math, is pretty cool' [PASSED]
Test Case '-[MathSpec Math_IsPrettyCool]' passed (0.001 seconds).
Test Suite 'MathSpec' finished at 2014-04-02 06:33:21 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 0.001 (0.001) seconds
Test Suite 'KiwiSampleTests.xctest' finished at 2014-04-02 06:33:21 +0000.
Executed 3 tests, with 0 failures (0 unexpected) in 0.347 (0.348) seconds
Test Suite 'All tests' finished at 2014-04-02 06:33:21 +0000.
Executed 3 tests, with 0 failures (0 unexpected) in 0.347 (0.349) seconds
** TEST SUCCEEDED **
```

動いた。
workspace使ってない場合、-workspace KiwiSample.xcworkspaceはいらない。
-destinationで指定しているデバイスは、[ここに書いてある](https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/iOS_Simulator_Guide/GettingStartedwithiOSStimulator/GettingStartedwithiOSStimulator.html#//apple_ref/doc/uid/TP40012848-CH5-SW1)文字列を入れてやれば良いっぽい。
2014/04/02時点では以下のとおり。

> iPhone Retina (3.5-inch)
iPhone Retina (4-inch)
iPhone Retina (4-inch 64-bit)
iPad
iPad Retina
iPad Retina (64-bit)

#Jenkinsでテスト結果を集計できるようにする
junit形式でレポートを生成しないといけないみたいなのでコンバーターを入れる。

```bash
$ gem install ocunit2junit
```

あとは、テスト実行コマンドの出力をリダイレクトしてやる。

```bash
$ xcodebuild -workspace KiwiSample.xcworkspace -scheme KiwiSample -destination 'platform=iOS Simulator,name=iPhone Retina (4-inch)' test 2>&1 | ocunit2junit
```

すると、test-reportsディレクトリが作成されその中にxmlファイルが生成される。
jenkinsでこのファイル群（test-reports/*.xml）を指定してやれば良い。