&tag(Xcode4/UnitTest);
*目次 [#l148bde4]
#contents
*参考情報 [#jd2c2448]
-[[https://developer.apple.com/jp/devcenter/ios/library/documentation/UnitTesting.pdf:https://developer.apple.com/jp/devcenter/ios/library/documentation/UnitTesting.pdf]]…Apple公式PDF
-[[Xcode 4 からはじめる Unit Testing - jarinosuke blog:http://blog.jarinosuke.com/entry/20120219/start_unit_testing_using_xcode4]]
-[[初めての単体テスト:http://www.slideshare.net/basuke/do-you-test-your-code]]
-[[[Xcode, Objective-C]SenTestingKitでユニットテスト | 学習B5デスノート:http://fernweh.jp/b/xcode-sentest/]]
-[["Include Unit Tests"をチェックし忘れたプロジェクトにUnitTestの設定をする - おかひろの雑記:http://d.hatena.ne.jp/okahiro_p/20120724/1343122150]]
*SenTestKit [#y50d49c2]
-Xcode4組み込みのテストキット。
-ロジックテストとアプリケーションテストに分けられる。
-デフォルトで作られるのはアプリケーションテスト。ドキュメントにはロジックテストはシミュレータ上でしか動かないとあるけど、アプリケーションテストもシミュレータ上で動いている。ログにAll testsとでているならアプリケーションテスト?


**Tips [#n1aebe9c]

***文字列の比較はSTAssertEqualObjectsで [#f2ed9401]
-STAssertEqualsじゃなく、STAssertEqualObjectsを使う。
 STAssertEqualObjects(@"2010/11/12 00:00:00", actual, @"compare 1");
***テストデータをテキストファイルから読み込む [#y2d60ee0]
-[[How to read a json file from unit test bundle in ios « The Agile Warrior:http://agilewarrior.wordpress.com/2012/10/23/how-to-read-a-json-file-from-unit-test-bundle-in-ios/]]が参考になる。[NSBundle bundleForClass:[self class]]がポイント。
-テスト用のjsonファイルjob.jsonがUnitTestsに存在し、テスト中に読み込みたい場合以下のようなヘルパーを用意しておく(参考URLではJSONデータを返しているが、以下ではNSString*としてそのまま返している)。
#pre{{
+ (NSString*)stringFromJSONFileNamed:(NSString *)fileName
{
    //    NSString *resource = [[NSBundle mainBundle] pathForResource:fileName ofType:@"json"];
    
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSString *resource = [bundle pathForResource:fileName ofType:@"json"];
    NSError *error;
    return [NSString stringWithContentsOfFile:resource encoding:NSUTF8StringEncoding error:&error];
}}
-テストメソッドでは以下のようにして呼び出す
#pre{{
- (void)testExample
{
    NSString* str = [TestingHelper stringFromJSONFileNamed:@"job"];
    NSLog(@"str=%@", str);
    //以下strを用いてテスト。
}
}}

*** 特定のテストだけ実行したい [#v04e1954]
-Edit Scheme→Testsで選択できる。
**トラブルシューティング [#dfbbc65b]
***boolの比較でこける [#q94d5ed3]
-[[assert - ios: OCUnit STAssertEquals -> Type missmatch - Stack Overflow:http://stackoverflow.com/questions/8406814/ios-ocunit-stassertequals-type-missmatch]]によるとboolではなく、BOOLを使わないといけないらしい。

***リンクエラー発生 [#xe2d4e9a]
-[[Adding Unit Tests to an existing iOS project with Xcode 4 | Two Bit Labs:http://twobitlabs.com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/]]に書いてあるように設定してあるか確認。Bundle Loaderなどが設定されてないとエラーになる。


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS