Contenu connexe Similaire à iOSアプリケーションの Unit Test (20) iOSアプリケーションの Unit Test2. iPhone SDK 勉強会
参考URL
iOS App Development Workflow Guide
http://developer.apple.com/library/ios/#documentation/Xcode/
Conceptual/ios_̲development_̲workflow/135-‐‑‒Unit_̲Testing_̲Applications/
unit_̲testing_̲applications.html
4. iPhone SDK 勉強会
なぜテストを書かないか
•めんどう
•(UIなどと⽐比べて) 効果がわかりにくい
•iPhoneアプリのテストってどう書いていいかわか
らない
etc.
5. iPhone SDK 勉強会
ツール/フレームワークを使う
•繰り返し実⾏行行するのが簡単
•⾃自動化できる
•テスト結果として⾒見見えるようになる
6. iPhone SDK 勉強会
無理理をしない
•モデルのテストは書きやすいし、論論理理構造が変わ
ることは少ないので、繰り返し実⾏行行するメリットが
⼤大きいのできちんと (正常、異異常、例例外、境界値、
etc) 書いたほうがいい
•いっぽう表⽰示については論論理理構造がひっくり返る
ような変更更もよくあるし、そもそも何が正しいとい
う状態を決定しにくいものなので、無理理に書こうと
しない。
7. iPhone SDK 勉強会
とはいえ、テストしやすい
コードを書く努⼒力力はしよう
•テストしやすいコードはほかから使いやすいコー
ドなので、良良い設計につながる。
(カメラの画像を加⼯工するようなメソッドは、画像の
取得と加⼯工を分ける、
位置情報を使う場合はラッパーを1枚はさむと⾃自由
な値を返しやすくなる、など)
8. iPhone SDK 勉強会
無理理をしない
- (id)init {
self = [super init];
if (self) {
service = [[LDRService alloc] init];
service.delegate = self;
}
return self;
[self
init]
nil
}
100%
9. iPhone SDK 勉強会
ツール/フレームワークを使う
•繰り返し実⾏行行するのが簡単
•⾃自動化できる
•テスト結果として⾒見見えるようになる
14. iPhone SDK 勉強会
OCUnit GHUnit
•Xcode標準 •Xcode⾮非標準
•XCodeと統合されたUI •テストランナーの独⾃自UI
•Assertマクロが必要最⼩小 •Assertマクロが若若⼲干多い
限しかない •setUpClass がある
•⾮非同期のテストが(その •⾮非同期のテストが可能
ままでは)できない •OCUnitのテストケースも実
⾏行行可能
•Viewの表⽰示テストができる
15. iPhone SDK 勉強会
OCUnit GHUnit
•コマンドラインで実⾏行行 •コマンドラインで実⾏行行
•CIとの連携 •CIとの連携
32. GHAssertNoErr(a1, description, ...)
GHAssertErr(a1, a2, description, ...)
iPhone SDK 勉強会
GHAssertNotNULL(a1, description, ...)
GHAssertNULL(a1, description, ...)
GHAssertNotEquals(a1, a2, description, ...)
GHAssertNotEqualObjects(a1, a2, desc, ...)
GHAssertOperation(a1, a2, op, description, ...)
GHAssertGreaterThan(a1, a2, description, ...)
GHAssertGreaterThanOrEqual(a1, a2, description, ...)
GHAssertLessThan(a1, a2, description, ...)
GHAssertLessThanOrEqual(a1, a2, description, ...)
GHAssertEqualStrings(a1, a2, description, ...)
GHAssertNotEqualStrings(a1, a2, description, ...)
GHAssertEqualCStrings(a1, a2, description, ...)
GHAssertNotEqualCStrings(a1, a2, description, ...)
GHAssertEqualObjects(a1, a2, description, ...)
GHAssertEquals(a1, a2, description, ...)
GHAbsoluteDifference(left,right) (MAX(left,right)-MIN(left,right))
GHAssertEqualsWithAccuracy(a1, a2, accuracy, description, ...)
GHFail(description, ...)
GHAssertNil(a1, description, ...)
GHAssertNotNil(a1, description, ...)
GHAssertTrue(expr, description, ...)
GHAssertTrueNoThrow(expr, description, ...)
GHAssertFalse(expr, description, ...)
GHAssertFalseNoThrow(expr, description, ...)
GHAssertThrows(expr, description, ...)
GHAssertThrowsSpecific(expr, specificException, description, ...)
GHAssertThrowsSpecificNamed(expr, specificException, aName, description, ...)
GHAssertNoThrow(expr, description, ...)
GHAssertNoThrowSpecific(expr, specificException, description, ...)
GHAssertNoThrowSpecificNamed(expr, specificException, aName, description, ...)
34. implementation MyTest
//
iPhone SDK 勉強会
- (void)setUp { }
//
- (void)tearDown { }
//
- (void)setUpClass { }
//
- (void)tearDownClass { }
// Tests are prefixed by 'test' and contain no arguments and no return
value
- (void)testA { }
// Another test; Tests are run in lexical order
- (void)testB { }
// Override any exceptions; By default exceptions are raised, causing a
test failure
- (void)failWithException:(NSException *)exception { }
@end
36. #import <GHUnitIOS/GHUnit.h>
iPhone SDK 勉強会
@interface LDRLoginTest : GHAsyncTestCase {
SEL currentTest;
}
@end
@implementation LDRLoginTest
- (void)testLoginSucceeded {
[self prepare];
currentTest = _cmd;
LDRService *service = [LDRService service];
service.delegate = self;
service.username = @"kishikawakatsumi";
service.password = @"password1234";
[service login];
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:10.0];
}
37. - (void)serviceLoginSucceeded:(LDRService *)service {
if (service.APIKey != nil) {
iPhone SDK 勉強会
[self notify:kGHUnitWaitStatusSuccess
forSelector:@selector(testLoginSucceeded)];
} else {
[self notify:kGHUnitWaitStatusFailure
forSelector:@selector(testLoginSucceeded)];
}
}
- (void)service:(LDRService *)service
loginFailedWithError:(NSError *)error {
[self notify:kGHUnitWaitStatusFailure
forSelector:@selector(testLoginSucceeded)];
}
@end
41. #import <GHUnitIOS/GHUnit.h>
iPhone SDK 勉強会
@interface LoginViewTest : GHViewTestCase
@end
#import "LoginViewTest.h"
#import "LoginViewController.h"
@implementation LoginViewTest
- (void)testLoginViewController {
LoginViewController *viewController =
[[LoginViewController alloc]
initWithNibName:@"LoginViewController" bundle:nil];
GHVerifyView(viewController.view);
}
@end
43. iPhone SDK 勉強会
まとめ
•iOS の 単体テストフレームワークはOCUnit と
GHUnit
•GHUnitがオススメ
•実⾏行行環境は1度度作っておけば汎⽤用的に使えるの
で覚えておいて損はない
51. iPhone SDK 勉強会
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc,
argv,
nil,
@"GHUnitIOSAppDelegate");
}
}