最近 DeviceWallpaper というアプリをリリースして、勉強のためテストも書いているためTravis CI と連携してテスト結果を README に表示させる手順をまとめてみる。
Travis CI は OSS なら無料で、 private は有料になっている。
Travis CI - Test and Deploy with Confidence
0. 前提
CocoaPods や Carthage はソース管理されていること。
ソース管理していない場合は毎回 Clone & Build が必要になるため、それらの設定を別途 .travis.yml に書く必要がある。
1. Sign Up と Switch ON
Travis CI から Sign Up する。
そのあと CI したいレポジトリのスイッチを ON にする。

設定
.travis.yml が無いと Build しないようにした。
しかし OSX Project の場合はいずれにしろ .travis.yml が無いと success することはない、 Linux で Build しようとしてしまうので。

2. fastlane test
テストは bundle exec fastlane test
で動くようにした。
Scheme の Shared をチェック
テストを実行するために Scheme を共有する必要がある。

fastlane
Scanfile
こんな感じで書いた、実物は Scanfile にある。
scheme "DeviceWallpaper"
configuration "Debug"
device "iPhone SE"
workspace "DeviceWallpaper.xcworkspace"
open_report true
clean true
skip_build true
output_types "html"
Fastfile
scan するだけ。実物は Fastfile にある。
lane :test do
scan
end
fastlane を使わない場合
Default Test Script に書いてある書き方でできそう、試してはいない。
3. Ruby version
Travis CI: Precompiled Ruby Versions にあるように、2017/05/14時点で Ruby 2.4.1 は使えないので 2.4.0 以下にする必要がある。
.travis.yml に rvm: 2.4.0
と書くか .ruby-version ファイルに 2.4.0 と書いておけば良い。
CocoaPods + Swift で Travis CI を使う - Qiita には .ruby-version があるとコケるとあったが、Using .ruby-version にあるように問題なく利用できる。
4. .travis.yml
Building an Objective-C or Swift Project - Travis CI を見ながら yml を書いた。
osx_image: xcode8.3
language: swift
script:
- bundle exec fastlane test --verbose
5. 結果
starhoshi/DeviceWallpaper - Travis CI
成功した

6. badge を README に貼る
Travis CI 上の build: passing
をクリックするとダイアログが出るので、それを Markdown でコピーして README に貼る。

すると こんな感じ で README に badge が表示される

実際に動くサンプル
GitHub - starhoshi/DeviceWallpaper: Device information wallpaper (VIPER Architecture)
Travis CI: starhoshi/DeviceWallpaper