LoginSignup
4
3

More than 5 years have passed since last update.

git commitをhookしてUnitTestを実行

Last updated at Posted at 2015-03-30

背景

githubを立てないような一人プロジェクトでもUnitテストを導入したいなと思いたった。
完全自分用メモ。

実装

pre-commit.samplepre-commitにリネーム後、下記を追加

shellScript
errors=$(xcodebuild -project {対象プロジェクト}.xcodeproj -scheme {テストスキーム} -destination 'platform=iOS Simulator,name={テスト対象のシミュレータ名}' test | grep 'XCT.* failed')

if [ -z "$errors" ]; then
    echo "test Success!!!"
else
    echo "\033[0;31m ********** Test Failed ********** \033[0;39m"
    echo "\033[0;31m${errors}\033[0;39m"
    echo "\033[0;31m ********** Test Failed ********** \033[0;39m"
    exit 1
fi

xcodebuildを使ってターミナルからテストを実行、結果から「XCT(ほにゃらら) failed」を含む行をgrepするという簡単なscriptです。

次にサンプルとして失敗するテストコードを用意。

swift
    func testExample() {
        XCTAssert(false, "test failed")
    }

実行結果

ターミナルから、git commitを行うとテストが実行されます。
テストに失敗した時は下記の形でターミナルに表示されます。

sc.png

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3