あいつの日誌β

働きながら旅しています。

iPhone:GHUnitでテスト駆動開発する手順

すっかりやり方を忘れてしまい時間を無駄にしてしまったので備忘録

GHUnitのダウンロード&ビルド

% git clone https://github.com/gabriel/gh-unit
% cd gh-unit/Project-iOS
% make

build が正常に終了したら build/Framework/GHUnitIOS.framework が作成されます。

% ls build/Framework

プロジェクトを作成

xcode で 適当なテストプジェクトを作成します。 例として Single View Application を作成します。

テスト用のターゲットを作成

例えば Tests というターゲット名で作成してください

Other Linker Flags

上記のターゲットの Other Linker Flagsに以下の2つを追加します。

  • -ObjC
  • -all_load

SEE ALSO: http://developer.apple.com/library/mac/#qa/qa1490/_index.html

QuartzCore.framework をプロジェクトに組み込む

QuartzCore.framework をプロジェクトに組み込みます。これは以下のディレクトリに存在します。

/System/Library/Frameworks/QuartzCore.framework

Show the project navigator の Frameworks フォルダを右クリック => Add File To "YourProject"を選択して上記のディレクトリを組み込みます。

以下の項目にチェックをしてください。ここでファイルをコピーすると依存関係がおかしくなるためか正常に動作しないためです。

  • Copy items into destination group’s folders のチェックをはずします
  • Create folder references for any added folders

GHUnitIOS.framework をプロジェクトに組み込む

先ほどの要領でプロジェクトに組み込みます。

ただし今回は以下の項目にチェックをしてください。

  • Copy items into destination group’s folders のチェックをつける
  • Create groups for any added folders

テスト用ターゲットの不要ファイルを削除

以下を削除します。

  • AppDelegate.m
  • AppDelegate.h
  • Support Files/infoPlist.strings

テスト用ターゲットにテストファイルを追加

ひとまず UnitTest を作成します。

UnitTest.h

#import <Foundation/Foundation.h>
#import <GHUnitIOS/GHUnit.h>

@interface UnitTest : GHTestCase {}
@end

UnitTest.m

@implementation UnitTest

- (void)testEquals {
    GHAssertEquals(1, 1, @"equal");
}

@end

main.m を修正

+    return UIApplicationMain(argc, argv, nil, @"GHUnitIOSAppDelegate");
-    return UIApplicationMain(argc, argv, nil, NSStringFromClass([BirdsAppDelegate class]));

実行

あとはRunボタンを押せばOKです。たぶん。